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 one 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#
Since you say invoke 2 controls at once without hanging the main thread, I suspect there is some confusion here.
When you
Invokea 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 .