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;
}