Answered Thread problem

tonytohme

New member
Joined
Sep 14, 2020
Messages
3
Programming Experience
5-10
Hey Fellas,



I encountered a problem that I have used successfully in past hundreds of time.

I started a thread which update a textbox on the form based on a value.

That value is being updated by the main class.

For some reason as soon as the loop to update the value goes in, the thread disappears and no longer updates the text box.

ANy input is much appreciated

Here's the code, the function initiating the thread is Button1_Click
C#:
using System;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;

public class InvokeThreadSafeForm : Form
{
private delegate void SafeCallDelegate();
private Button button1;
private TextBox textBox1;
private Thread thread2 = null;
private int Diff = 0;
[STAThread]
static void Main()
{
Application.SetCompatibleTextRenderingDefault(false);
Application.EnableVisualStyles();
Application.Run(new InvokeThreadSafeForm());
}
public InvokeThreadSafeForm()
{
button1 = new Button
{
Location = new Point(15, 55),
Size = new Size(240, 20),
Text = "Set text safely"
};
button1.Click += new EventHandler(Button1_Click);
textBox1 = new TextBox
{
Location = new Point(15, 15),
Size = new Size(240, 20)
};
Controls.Add(button1);
Controls.Add(textBox1);
}

private void Button1_Click(object sender, EventArgs e)
{
thread2 = new Thread(new ThreadStart(SetText));
thread2.Start();
while( true )
        {   

                Thread.Sleep(1000);
Diff++;
}

}

private void WriteTextSafe()
{
if (textBox1.InvokeRequired)
{
var d = new SafeCallDelegate(WriteTextSafe);
textBox1.Invoke(d);
}
else
{
textBox1.Text = Diff.ToString();
}
}

private void SetText()
{
while (true)
{
WriteTextSafe();
}
}
}
 
Last edited by a moderator:
Welcome to the forums. In the future please place code within code tags. It makes it easier for people to view your code within the forum.
 
The issue is that you have locked out the message pump on the main thread by having an infinite loop on line 42-47. The other Windows messages never get sent through. textBox1.Invoke() actually posts a message back to the main form so that it will be run on the main thread. But with the main message pump not running, the message never gets delivered.

And no, using the VB6 approach of using Application.DoEvent() is not the correct solution because you now set yourself up for hard to reproduce and debug re-entrancy issues.

The correct solution is to create a timer to increment the value.

You could also use another thread if you wish. In general, though, remember that threads are a limited Windows resource, and context switching between threads is expensive. In a small toy application, this isn't an issue, but as your code get bigger, this can become a resource drain. The background worker is more appropriate since you also don't have to worry as much about cross-thread calls.
 
Thank you for responding, it make sense what you said, and I get it. I guess the question then, how can I have my thread monitors a value that the main thread is updating continiously.
The above code was an example, the actual logic I have is a function that is processing a dataviewgrid of about 70K records (rows), and I want to display a real time counter of progress of the current record being processed. So if the function processing the rows is locking out the main thread how can the update textbox thread reacts?

thanks again,
 
Back
Top Bottom