I have a multi-threaded application in C# which tries to write to a TextBox in a Windows.Forms created by another thread.
我在c#中有一个多线程应用程序,它试图在Windows中写入文本框。由另一个线程创建的表单。
As threads cannot modify what has not been created by them, I was using InvokeRequired as shown on the code below, to solve this problem.
由于线程不能修改未由它们创建的内容,因此我使用InvokeRequired(如下面的代码所示)来解决这个问题。
public delegate void MessageDelegate(Communication message);
void agent_MessageReceived(Communication message)
{
if (InvokeRequired)
{
BeginInvoke(new MessageDelegate(agent_MessageReceived), new object[] { message });
}
else
{
TextBox1.Text += message.Body;
}
}
Now I need to do the same for a TextBox in an ASP.NET app, but apparently neither InvokeRequired nor BeginInvoke exist for TextBox in a Web.UI. What can I do?
现在我需要对ASP中的文本框执行相同的操作。NET应用程序,但显然Web.UI中的TextBox既不存在InvokeRequired也不存在BeginInvoke方法。我能做什么?
2 个解决方案
#1
0
If you're trying to do what I think, then it's not possible. If your event handler is being called by some background process on the server then how could you possibly update a text box? Which text box would you be updating? The text boxes you are trying to update are all on the client's web browser, not your server.
如果你想做我认为的事,那是不可能的。如果您的事件处理程序在服务器上被某个后台进程调用,那么您怎么可能更新一个文本框呢?您要更新哪个文本框?您正在尝试更新的文本框都在客户机的web浏览器上,而不是您的服务器上。
Anyhow, the answer to your question is that there is no InvokeRequired or Invoke method because in ASP.NET controls cannot be accessed across threads. Only a thread currently processing a web request can access the elements of the page being served.
无论如何,您的问题的答案是没有InvokeRequired或Invoke方法,因为在ASP中。不能跨线程访问NET控件。只有当前处理web请求的线程才能访问所服务页面的元素。
You'll need to rethink your approach. You can't really do things in a web application the same way that you would a desktop application.
你需要重新考虑你的方法。在web应用程序中,您不能像在桌面应用程序中那样做事情。
#2
2
I think that you don't need this on Web UI. Each client receives its own TextBox and the WebServer does not reuse the TextBox for more than one request. Do I miss something here?
我认为在Web UI中不需要这个。每个客户端都会收到自己的文本框,而WebServer不会重复使用文本框。我是不是漏掉了什么?
#1
0
If you're trying to do what I think, then it's not possible. If your event handler is being called by some background process on the server then how could you possibly update a text box? Which text box would you be updating? The text boxes you are trying to update are all on the client's web browser, not your server.
如果你想做我认为的事,那是不可能的。如果您的事件处理程序在服务器上被某个后台进程调用,那么您怎么可能更新一个文本框呢?您要更新哪个文本框?您正在尝试更新的文本框都在客户机的web浏览器上,而不是您的服务器上。
Anyhow, the answer to your question is that there is no InvokeRequired or Invoke method because in ASP.NET controls cannot be accessed across threads. Only a thread currently processing a web request can access the elements of the page being served.
无论如何,您的问题的答案是没有InvokeRequired或Invoke方法,因为在ASP中。不能跨线程访问NET控件。只有当前处理web请求的线程才能访问所服务页面的元素。
You'll need to rethink your approach. You can't really do things in a web application the same way that you would a desktop application.
你需要重新考虑你的方法。在web应用程序中,您不能像在桌面应用程序中那样做事情。
#2
2
I think that you don't need this on Web UI. Each client receives its own TextBox and the WebServer does not reuse the TextBox for more than one request. Do I miss something here?
我认为在Web UI中不需要这个。每个客户端都会收到自己的文本框,而WebServer不会重复使用文本框。我是不是漏掉了什么?