开发者

Help needed with InvokeRequired for Web.UI

开发者 https://www.devze.com 2022-12-26 16:43 出处:网络
I have a multi-threaded application in C# which tries to write to a TextBox in a Windows.Forms created by another thread.

I have a multi-threaded application in C# which tries to write to a TextBox in a Windows.Forms created by another thread.

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.

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?


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?


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.

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.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消