Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Using Thread is not successful

Using Thread is not successful

Scheduled Pinned Locked Moved C#
question
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    taibc
    wrote on last edited by
    #1

    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,

    L T 2 Replies Last reply
    0
    • T taibc

      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,

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      • T taibc

        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,

        T Offline
        T Offline
        Trak4Net
        wrote on last edited by
        #3

        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
           }
        
        T 2 Replies Last reply
        0
        • T Trak4Net

          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
             }
          
          T Offline
          T Offline
          taibc
          wrote on last edited by
          #4

          Great ! It is successful now. Thanks Trak4Net very much.:thumbsup:

          1 Reply Last reply
          0
          • T Trak4Net

            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
               }
            
            T Offline
            T Offline
            taibc
            wrote on last edited by
            #5

            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,

            T 1 Reply Last reply
            0
            • T taibc

              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,

              T Offline
              T Offline
              Trak4Net
              wrote on last edited by
              #6

              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");

              T 1 Reply Last reply
              0
              • T Trak4Net

                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");

                T Offline
                T Offline
                taibc
                wrote on last edited by
                #7

                Thank you. I resolved this error earlier. Kind regards, Tai

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups