0

I am trying to use a background thread in my C# WebForm application (This is NOT a Windows Form issue). The thread monitors a TCP/IP connection looking for data to appear on the receive side. When the data appears I want to write it to a textbox on my webpage. The problem is that when I try to write to the textbox from this thread nothing actually gets written to the textbox and then the webpage loads. I know this is a cross thread issue and not a page loading issue.

My program waits for the data to be written to "a textbox" before rendering the webpage. I tried using a delegate but that didn't work. Also I have searched for a solution but most answers deal with Windows Forms and WebForms seem to act differently.

For my C# WebForm application, how do I get access to the data being created in a background thread so I can load it to a textbox on my webpage?

Thread ipThread;

protected void Page_Load(object sender, EventArgs e)
{
  ipThread = new Thread(() => ipSocketRcv();
  ipThread.IsBackground = true;
  ipThread.Priority = ThreadPriority.BelowNormal;
  ipThread.Start();
}

void ipSocketRcv()
{
  while (true)
  {
     if receive data...
       myTextBox.Text = data;  **//This does not work**
       finished = true;
  }
}

protected void SendCmd_Click(object sender, EventArgs e)
{
    //send command over ip connection when button pressed           
    waitToLoad(); //Then wait for data to be received before rendering page
}

private void waitToLoad()
{
    while (!finished)
    {
        Thread.Sleep(0);
    }
    finished = false;
}
Viper
  • 3
  • 3
  • Possible duplicate of [Writing to a TextBox from another thread?](http://stackoverflow.com/questions/519233/writing-to-a-textbox-from-another-thread) – John Wu Dec 14 '16 at 00:09
  • 1
    This is not a duplicate as the question is about the webforms. – VMAtm Dec 14 '16 at 03:56

1 Answers1

2

This approach will not work in WebForms, as after request handling all the threads are suspended in a waiting for a new request. You should introduce a polling javascript, which will call the server side for an update.

Also you should move out from the web application your ip socket receiver. Your options are some kind of daemon (windows service) or WCF binding.

VMAtm
  • 27,943
  • 17
  • 79
  • 125
  • However the way I have implemented the thread is that for the most part it does all of the work during a request. There are only a few occasions when I would see unsolicited data on the ip socket receiver (which was the only reason I thought a thread was a good idea). Still I should be able to get the data from the thread and put it in a textbox, correct? I know InvokeRequired does not work. Suggestions... – Viper Dec 14 '16 at 16:23
  • Your thread do work only during request handling. So, *probably*, you can get some data from ip socket will then, but I wouldn't rely on this. Create a ajax call to some kind of service on the server side and update the textbox with jquery. – VMAtm Dec 14 '16 at 16:37