Cross threaded operations
-
I have recently started to multithread my program and everything seems to work fine with the exception of a form I use to track the progress of data being transfered. The Progress form is on the main thread, I send data to it as my program is processing it. The problem is that is does not appear on the form, I have used various methods to Invoke the thread but it just goes into a loop and never stops or just goes into oblivian. Some of the methods are as follows or similar:
public void UpdateStartTime(String text) { if (textBoxStartTime.InvokeRequired) { UpdateStartTimeCallback updateStartTimeCallback = new UpdateStartTimeCallback(UpdateStartTime); Invoke(updateStartTimeCallback, new object\[\] { Text }); } else { textBoxStartTime.Text = text; } }
and
public void UpdateStartTime(TextBox textBox, String text) { if (textBoxStartTime.InvokeRequired) { textBoxStartTime.Invoke(new Action(UpdateStartTime), new object\[\] { textBox, text }); } else { textBoxStartTime.Text = text; } }
I also tried a delegate but it just does not see the other thread, I have verified the thread numbers are different and they're being used, it just doesn't make the connection. Any ideas or suggestions will be greatly appreciated, thanks in advance. Michael
-
I have recently started to multithread my program and everything seems to work fine with the exception of a form I use to track the progress of data being transfered. The Progress form is on the main thread, I send data to it as my program is processing it. The problem is that is does not appear on the form, I have used various methods to Invoke the thread but it just goes into a loop and never stops or just goes into oblivian. Some of the methods are as follows or similar:
public void UpdateStartTime(String text) { if (textBoxStartTime.InvokeRequired) { UpdateStartTimeCallback updateStartTimeCallback = new UpdateStartTimeCallback(UpdateStartTime); Invoke(updateStartTimeCallback, new object\[\] { Text }); } else { textBoxStartTime.Text = text; } }
and
public void UpdateStartTime(TextBox textBox, String text) { if (textBoxStartTime.InvokeRequired) { textBoxStartTime.Invoke(new Action(UpdateStartTime), new object\[\] { textBox, text }); } else { textBoxStartTime.Text = text; } }
I also tried a delegate but it just does not see the other thread, I have verified the thread numbers are different and they're being used, it just doesn't make the connection. Any ideas or suggestions will be greatly appreciated, thanks in advance. Michael
try like this. define a simple delegate in your class and call it like below. delegate definition
public delegate void StatusUpdateHandler(string status)
use the delegate in your application like this
public void UpdateStartTime(string text)
{
if (this.InvokeRequired)
{
BeginInvoke(new StatusUpdateHandler(UpdateStartTime),new object[]{text} ); // this will call the UpdateStartTime method again, but second time you are routing your execution to main thread, InvokeRequired flag will be false and else part gets executed.
}
else
{
textBoxStartTime.Text = text;
}}
Jibesh V P
-
try like this. define a simple delegate in your class and call it like below. delegate definition
public delegate void StatusUpdateHandler(string status)
use the delegate in your application like this
public void UpdateStartTime(string text)
{
if (this.InvokeRequired)
{
BeginInvoke(new StatusUpdateHandler(UpdateStartTime),new object[]{text} ); // this will call the UpdateStartTime method again, but second time you are routing your execution to main thread, InvokeRequired flag will be false and else part gets executed.
}
else
{
textBoxStartTime.Text = text;
}}
Jibesh V P
-
I tried the above exactly as shown, however it does not return to the method. Any other ideas.
That's the standard pattern. If it's not working, there's something you haven't told us and/or are not showing us. The code listed in the first reply is the way to do it, so long as the controls were created on the UI thread and your long-running code is on a different thread, be it directly launched with the Thread class, a Task or in a BackgroundWorker.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak