Using Thread is not successful
-
Hi Friends, I am trying to create a new thread to make the picturebox is visible (to show Loading icon) when the ConvertFiles method is running, but it is not successful (Please see below codes) Do you know what's wrong ?
private void ShowLoadingIcon()
{
if (InvokeRequired)
{
this.Invoke (new MethodInvoker( ShowLoadingIcon));} else { pixLoadingIcon.Visible = true; } }
private void btnConvert_Click(object sender, EventArgs e)
{Thread thLoading = new Thread(ShowLoadingIcon); thLoading.Start(); // This method will take a few minutes ConvertFiles(sourceFile, destinationFile); }
Thanks and regards,
-
Hi Friends, I am trying to create a new thread to make the picturebox is visible (to show Loading icon) when the ConvertFiles method is running, but it is not successful (Please see below codes) Do you know what's wrong ?
private void ShowLoadingIcon()
{
if (InvokeRequired)
{
this.Invoke (new MethodInvoker( ShowLoadingIcon));} else { pixLoadingIcon.Visible = true; } }
private void btnConvert_Click(object sender, EventArgs e)
{Thread thLoading = new Thread(ShowLoadingIcon); thLoading.Start(); // This method will take a few minutes ConvertFiles(sourceFile, destinationFile); }
Thanks and regards,
taibc wrote:
but it is not successful
What does this mean? The thread does not start? The thread starts but does not do anything? The code runs and your cat died? "it's not successful" does not tell us anything about what your problem is. Did you run it through a debugger? What did you discover?
Why is common sense not common? Never argue with an idiot. They will drag you down to their level where they are an expert. Sometimes it takes a lot of work to be lazy Please stand in front of my pistol, smile and wait for the flash - JSOP 2012
-
Hi Friends, I am trying to create a new thread to make the picturebox is visible (to show Loading icon) when the ConvertFiles method is running, but it is not successful (Please see below codes) Do you know what's wrong ?
private void ShowLoadingIcon()
{
if (InvokeRequired)
{
this.Invoke (new MethodInvoker( ShowLoadingIcon));} else { pixLoadingIcon.Visible = true; } }
private void btnConvert_Click(object sender, EventArgs e)
{Thread thLoading = new Thread(ShowLoadingIcon); thLoading.Start(); // This method will take a few minutes ConvertFiles(sourceFile, destinationFile); }
Thanks and regards,
I think you are doing this backwards. The invoke back to the UI thread will be blocked until your long processing task is finished. You should actually be trying to do something like this
string sourceFile, destinationFile;
private void ShowLoadingIcon() { if (InvokeRequired) { this.Invoke(new MethodInvoker(ShowLoadingIcon)); } else { pixLoadingIcon.Visible = true; } } private void btnConvert\_Click(object sender, EventArgs e) { ShowLoadingIcon(); //start thread to process long running task System.Threading.Thread t = new System.Threading.Thread(() => { ConvertFiles(sourceFile, destinationFile); }); t.Start(); } private void ConvertFiles(string src, string dest) { //long running task to convert files }
-
I think you are doing this backwards. The invoke back to the UI thread will be blocked until your long processing task is finished. You should actually be trying to do something like this
string sourceFile, destinationFile;
private void ShowLoadingIcon() { if (InvokeRequired) { this.Invoke(new MethodInvoker(ShowLoadingIcon)); } else { pixLoadingIcon.Visible = true; } } private void btnConvert\_Click(object sender, EventArgs e) { ShowLoadingIcon(); //start thread to process long running task System.Threading.Thread t = new System.Threading.Thread(() => { ConvertFiles(sourceFile, destinationFile); }); t.Start(); } private void ConvertFiles(string src, string dest) { //long running task to convert files }
-
I think you are doing this backwards. The invoke back to the UI thread will be blocked until your long processing task is finished. You should actually be trying to do something like this
string sourceFile, destinationFile;
private void ShowLoadingIcon() { if (InvokeRequired) { this.Invoke(new MethodInvoker(ShowLoadingIcon)); } else { pixLoadingIcon.Visible = true; } } private void btnConvert\_Click(object sender, EventArgs e) { ShowLoadingIcon(); //start thread to process long running task System.Threading.Thread t = new System.Threading.Thread(() => { ConvertFiles(sourceFile, destinationFile); }); t.Start(); } private void ConvertFiles(string src, string dest) { //long running task to convert files }
Hi Trak4Net, When running the new thread, I got the bellow error: "{Text = '((System.Windows.Forms.TextBox)(txtLopDiaChi)).Text' threw an exception of type 'Microsoft.VisualStudio.Debugger.Runtime.CrossThreadMessagingException'} System.Windows.Forms.TextBox " Do you know how to fix it ? Thanks and regards,
-
Hi Trak4Net, When running the new thread, I got the bellow error: "{Text = '((System.Windows.Forms.TextBox)(txtLopDiaChi)).Text' threw an exception of type 'Microsoft.VisualStudio.Debugger.Runtime.CrossThreadMessagingException'} System.Windows.Forms.TextBox " Do you know how to fix it ? Thanks and regards,
You should read up about making thread-safe calls to controls here is one link http://msdn.microsoft.com/en-us/library/ms171728.aspx[^] Anytime you access a control from a thread other than the thread the control was created on you will need to use Invoke or BeginInvoke to modify. Usually if there are several common controls I create a method that accepts a control as a parameter as well as the text to set and inside that method it checks if invoke is required. Here is an example of something you could do. You don't show enough code to know exactly what you are trying but this can be modified to work with different controls etc. Hope this is helpful.
/// /// delegate to invoke when invoke required /// private delegate void ChangeTextDel(System.Windows.Forms.TextBox obj, string newtext); /// /// Changes the text. /// /// The obj. /// The newtext. private void ChangeText(System.Windows.Forms.TextBox obj, string newtext) { if (obj.InvokeRequired) { obj.Invoke(new ChangeTextDel(ChangeText), new object\[\] { obj, newtext }); } else { obj.Text = newtext; } }
//from thread
ChangeText(txtLopDiaChi, "my new text value"); -
You should read up about making thread-safe calls to controls here is one link http://msdn.microsoft.com/en-us/library/ms171728.aspx[^] Anytime you access a control from a thread other than the thread the control was created on you will need to use Invoke or BeginInvoke to modify. Usually if there are several common controls I create a method that accepts a control as a parameter as well as the text to set and inside that method it checks if invoke is required. Here is an example of something you could do. You don't show enough code to know exactly what you are trying but this can be modified to work with different controls etc. Hope this is helpful.
/// /// delegate to invoke when invoke required /// private delegate void ChangeTextDel(System.Windows.Forms.TextBox obj, string newtext); /// /// Changes the text. /// /// The obj. /// The newtext. private void ChangeText(System.Windows.Forms.TextBox obj, string newtext) { if (obj.InvokeRequired) { obj.Invoke(new ChangeTextDel(ChangeText), new object\[\] { obj, newtext }); } else { obj.Text = newtext; } }
//from thread
ChangeText(txtLopDiaChi, "my new text value");