MultiThreading Windows Application
Lets say i have a form with 2 textboxs
im in a different thread and i want to compine texts in those two textboxs i understand that i can invoke each one of them and get the value then concatenate them but
i was thinking of a way to invoke 2 controls at once without hanging the main thread.
, i tried invoking textboxes on开发者_StackOverflow社区e by one then get values but that hanged my main thread for some reasons
any idea of how to do that without hanging the main thread ?
Sample:
txtFrom.Invoke(new MethodInvoker(() => strFrom = txtFrom.Text));
txtTo.Invoke(new MethodInvoker(() => strTo = txtTo.Text));
txtMessageBody.Invoke(new MethodInvoker(() => strMessageBody = txtMessageBody.Text));
txtStartDate.Invoke(new MethodInvoker(() => strStartDate = txtStartDate.Text));
txtEndDate.Invoke(new MethodInvoker(() => strEndDate = txtEndDate.Text));
ddlStatus.Invoke(new MethodInvoker(() => intStatus = ddlStatus.SelectedIndex));
thanks in advance
Enviroments:
Windows 7. .Net 4 Windows Form C#
Can you use BeginInvoke
instead? I always avoid Invoke
because it has a habit of causing deadlocks, which will hang the application...
The reason for this is that Invoke
makes your worker thread sit and wait until the UI thread is available. That can be a problem because there's a good chance that your UI thread was sitting waiting for your worker thread to do something. (For example, your worker thread may be in possession of locks, possibly ones you don't even know anything about because they are inside some library component. Implicit acquisition of locks or other unshareable resources can cause deadlocks even in single-thread code so it's best to be careful. It's almost always a terrible idea to make a worker thread wait for the UI thread to do something.)
As Peter Lillevold points out, ultimately you need to make sure you don't do anything slow on the UI thread. But it's not clear from your example code what's going on - I don't see anything obvious in your code that'll be slow, and from your description, it sounded like the application hangs completely, in which case it's more likely that you've got an Invoke-induced deadlock.
Since they live in the same form, they will all belong to the same GUI thread. This means that you can use any of them (or their parent form) as the invoke target, and do all operations within one invoke:
this.Invoke(new MethodInvoker(() =>
{
strFrom = txtFrom.Text;
strTo = txtTo.Text;
strMessageBody = txtMessageBody.Text;
strStartDate = txtStartDate.Text;
strEndDate = txtEndDate.Text;
intStatus = ddlStatus.SelectedIndex;
}));
I am not sure this solves your problem, but it at least simplifies your code.
Since you say invoke 2 controls at once without hanging the main thread, I suspect there is some confusion here.
When you Invoke
a control in WinForms as you describe, code within those MethodInvokes is actually executed on the UI/main thread. This is by design, invoking those controls will execute code on the UI thread so that the controls may be accessed on the thread they were created by originally. Thus, any lengthy operation in that code will hang the UI thread.
The lengthy operation should therefore be started on a separate thread, perhaps using the BackgroundWorker .
精彩评论