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. Threading

Threading

Scheduled Pinned Locked Moved C#
helpquestion
6 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.
  • M Offline
    M Offline
    MumbleB
    wrote on last edited by
    #1

    Hi Guys. I have a slight problem. I have written an app that processes various types of files. I create a Thread for each type of file being processed depending on a checkbox being checked. Now, from each file processed I output totals to a label on the form. However, this only seems to work for the first two files being processed and the remainder does not get their totals output to the labels. Is this a known issue and is there a way I can get around this? The code is kinda long so don't really want to add any here now. But the process flow of the program is such. 1) User selects a searches for file to process. 2) User checks the appropriate checkbox. 3) Thread is started calling the block of code to run. 4) Once file is processed finalize by calling GC.Collect(). if user wants to process another file he unchecks the current chechbox, checks a new checkbox and searches for new file and above process is followed calling the appropriate Thread to run. Can somebody advise why when running more than two "Thread" processes in succession results in the third and fourth not outputting totals to the Label's?

    Excellence is doing ordinary things extraordinarily well.

    OriginalGriffO 1 Reply Last reply
    0
    • M MumbleB

      Hi Guys. I have a slight problem. I have written an app that processes various types of files. I create a Thread for each type of file being processed depending on a checkbox being checked. Now, from each file processed I output totals to a label on the form. However, this only seems to work for the first two files being processed and the remainder does not get their totals output to the labels. Is this a known issue and is there a way I can get around this? The code is kinda long so don't really want to add any here now. But the process flow of the program is such. 1) User selects a searches for file to process. 2) User checks the appropriate checkbox. 3) Thread is started calling the block of code to run. 4) Once file is processed finalize by calling GC.Collect(). if user wants to process another file he unchecks the current chechbox, checks a new checkbox and searches for new file and above process is followed calling the appropriate Thread to run. Can somebody advise why when running more than two "Thread" processes in succession results in the third and fourth not outputting totals to the Label's?

      Excellence is doing ordinary things extraordinarily well.

      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #2

      Without some idea of the code it is really difficult to suggest anything, but there general things you can check: Are the thread running at all? Add some logging to a file to record what is being done, and in what order. Are the output overwriting each other? Try logging the outputs as well. Are the thread having problems with conflicts? Are there any locks which might be waiting? Is it always the same threads that don't run? Or does that change depending on what order they are started in, say? Sorry not to give concrete answers, but there is a heck of a lot that could be going wrong here - I think your first call is to gather more info!

      If Barbie is so popular, why do you have to buy her friends? Eagles may soar, but weasels don't get sucked into jet engines. If at first you don't succeed, destroy all evidence that you tried.

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      M 1 Reply Last reply
      0
      • OriginalGriffO OriginalGriff

        Without some idea of the code it is really difficult to suggest anything, but there general things you can check: Are the thread running at all? Add some logging to a file to record what is being done, and in what order. Are the output overwriting each other? Try logging the outputs as well. Are the thread having problems with conflicts? Are there any locks which might be waiting? Is it always the same threads that don't run? Or does that change depending on what order they are started in, say? Sorry not to give concrete answers, but there is a heck of a lot that could be going wrong here - I think your first call is to gather more info!

        If Barbie is so popular, why do you have to buy her friends? Eagles may soar, but weasels don't get sucked into jet engines. If at first you don't succeed, destroy all evidence that you tried.

        M Offline
        M Offline
        MumbleB
        wrote on last edited by
        #3

        Hi Griff. The Threads are all running. Output is created from all the Threads. However, I display some totals on screen for the user to verify against. It will output the totals for two of the threads run after each other, but when running the third and fourth thread I get no totals output to screen. I have tested by adding the totals to a messagebox after it is supposed to be output to the label and that works. Below is the code. It might be long but this is what I have.

           #region Set Text Code
        
            public delegate void SetText(Control ctrl, string str);
            public delegate void SetLabel(Control lblctrl, string filename);
            private delegate void updateBar();
        
            private void setText(Control ctrl, string str)
            {
                if (this.WindowState == FormWindowState.Minimized)
                    return;
                if (ctrl.InvokeRequired)
                    ctrl.BeginInvoke(new SetText(setText), new object\[\] { ctrl, str });
                else
                    ctrl.Text = str;
            }
        
            private void setFilename(Control lblctrl, string filename)
            {
                if (this.WindowState == FormWindowState.Minimized)
                    return;
                if (lblctrl.InvokeRequired)
                    lblctrl.BeginInvoke(new SetLabel(setFilename), new object\[\] { lblctrl, filename });
                else
                    lblctrl.Text = filename;
            }
        
            private void finalizeProcess()
            {
                //progressBar1.Style = ProgressBarStyle.Blocks;
                //progressBar1.Value = 0;
                //progressBar1.Dispose();
                GC.Collect();
                //base.Close();
            }
        
            #endregion
        
            #region Open File Dialog Code
        
            private void button3\_Click(object sender, EventArgs e)
            {
                if (openFileDialog1.ShowDialog() != DialogResult.OK)
                {
                    return;
                }
                else
                {
                    txtboxSelectFile.Text = openFileDialog1.FileName;
                }
        
            }
        
            #endregion
        
            #region Select File Code
        
            private void btnStart\_Click(object sender, EventArgs e)
            {
                setText(this, "Checking Process Selection");
                if (chkboxHolders.CheckState == CheckState.Unchecked && chkboxCert.CheckState == CheckState.Unchecked && 
                    chkboxPiadd.CheckState == CheckState.Unchecked && chkboxDivRecords.CheckState == CheckState.Unchecked)
                {
        
        OriginalGriffO L 2 Replies Last reply
        0
        • M MumbleB

          Hi Griff. The Threads are all running. Output is created from all the Threads. However, I display some totals on screen for the user to verify against. It will output the totals for two of the threads run after each other, but when running the third and fourth thread I get no totals output to screen. I have tested by adding the totals to a messagebox after it is supposed to be output to the label and that works. Below is the code. It might be long but this is what I have.

             #region Set Text Code
          
              public delegate void SetText(Control ctrl, string str);
              public delegate void SetLabel(Control lblctrl, string filename);
              private delegate void updateBar();
          
              private void setText(Control ctrl, string str)
              {
                  if (this.WindowState == FormWindowState.Minimized)
                      return;
                  if (ctrl.InvokeRequired)
                      ctrl.BeginInvoke(new SetText(setText), new object\[\] { ctrl, str });
                  else
                      ctrl.Text = str;
              }
          
              private void setFilename(Control lblctrl, string filename)
              {
                  if (this.WindowState == FormWindowState.Minimized)
                      return;
                  if (lblctrl.InvokeRequired)
                      lblctrl.BeginInvoke(new SetLabel(setFilename), new object\[\] { lblctrl, filename });
                  else
                      lblctrl.Text = filename;
              }
          
              private void finalizeProcess()
              {
                  //progressBar1.Style = ProgressBarStyle.Blocks;
                  //progressBar1.Value = 0;
                  //progressBar1.Dispose();
                  GC.Collect();
                  //base.Close();
              }
          
              #endregion
          
              #region Open File Dialog Code
          
              private void button3\_Click(object sender, EventArgs e)
              {
                  if (openFileDialog1.ShowDialog() != DialogResult.OK)
                  {
                      return;
                  }
                  else
                  {
                      txtboxSelectFile.Text = openFileDialog1.FileName;
                  }
          
              }
          
              #endregion
          
              #region Select File Code
          
              private void btnStart\_Click(object sender, EventArgs e)
              {
                  setText(this, "Checking Process Selection");
                  if (chkboxHolders.CheckState == CheckState.Unchecked && chkboxCert.CheckState == CheckState.Unchecked && 
                      chkboxPiadd.CheckState == CheckState.Unchecked && chkboxDivRecords.CheckState == CheckState.Unchecked)
                  {
          
          OriginalGriffO Offline
          OriginalGriffO Offline
          OriginalGriff
          wrote on last edited by
          #4

          Without actually running it, I can't see anything obviously wrong. Since this is the code that (doesn't) show the problem, I would be tempted to try

              private void setFilename(Control lblctrl, string filename)
              {
                  Console.WriteLine("setFileName Entered: " + lblCtrl.Name + " : " + filename);
                  if (this.WindowState == FormWindowState.Minimized)
                      return;
                  Console.WriteLine("setFileName PreInvokeCheck: " + lblCtrl.Name + " : " + filename);
                  if (lblctrl.InvokeRequired)
                      {
                      Console.WriteLine("setFileName BeginInvoke: " + lblCtrl.Name + " : " + filename);
                      lblctrl.BeginInvoke(new SetLabel(setFilename), new object\[\] { lblctrl, filename });
                      }
                  else
                      {
                      Console.WriteLine("setFileName Direct: " + lblCtrl.Name + " : " + filename);
                      lblctrl.Text = filename;
                      }
              }
          

          Or equivilent to check if it gets to the "setFileName Direct" bit when you don't get the label written. It may give a clue to what is going on (or worse, remove the problem)

          If Barbie is so popular, why do you have to buy her friends? Eagles may soar, but weasels don't get sucked into jet engines. If at first you don't succeed, destroy all evidence that you tried.

          "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
          "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

          M 1 Reply Last reply
          0
          • OriginalGriffO OriginalGriff

            Without actually running it, I can't see anything obviously wrong. Since this is the code that (doesn't) show the problem, I would be tempted to try

                private void setFilename(Control lblctrl, string filename)
                {
                    Console.WriteLine("setFileName Entered: " + lblCtrl.Name + " : " + filename);
                    if (this.WindowState == FormWindowState.Minimized)
                        return;
                    Console.WriteLine("setFileName PreInvokeCheck: " + lblCtrl.Name + " : " + filename);
                    if (lblctrl.InvokeRequired)
                        {
                        Console.WriteLine("setFileName BeginInvoke: " + lblCtrl.Name + " : " + filename);
                        lblctrl.BeginInvoke(new SetLabel(setFilename), new object\[\] { lblctrl, filename });
                        }
                    else
                        {
                        Console.WriteLine("setFileName Direct: " + lblCtrl.Name + " : " + filename);
                        lblctrl.Text = filename;
                        }
                }
            

            Or equivilent to check if it gets to the "setFileName Direct" bit when you don't get the label written. It may give a clue to what is going on (or worse, remove the problem)

            If Barbie is so popular, why do you have to buy her friends? Eagles may soar, but weasels don't get sucked into jet engines. If at first you don't succeed, destroy all evidence that you tried.

            M Offline
            M Offline
            MumbleB
            wrote on last edited by
            #5

            Thanks mate. I will give it a go and post back. It is just strange that it does it for two consecutive runs but for the third and fourth it doesn't. And it doesn't matter in what order it is run or not.

            Excellence is doing ordinary things extraordinarily well.

            1 Reply Last reply
            0
            • M MumbleB

              Hi Griff. The Threads are all running. Output is created from all the Threads. However, I display some totals on screen for the user to verify against. It will output the totals for two of the threads run after each other, but when running the third and fourth thread I get no totals output to screen. I have tested by adding the totals to a messagebox after it is supposed to be output to the label and that works. Below is the code. It might be long but this is what I have.

                 #region Set Text Code
              
                  public delegate void SetText(Control ctrl, string str);
                  public delegate void SetLabel(Control lblctrl, string filename);
                  private delegate void updateBar();
              
                  private void setText(Control ctrl, string str)
                  {
                      if (this.WindowState == FormWindowState.Minimized)
                          return;
                      if (ctrl.InvokeRequired)
                          ctrl.BeginInvoke(new SetText(setText), new object\[\] { ctrl, str });
                      else
                          ctrl.Text = str;
                  }
              
                  private void setFilename(Control lblctrl, string filename)
                  {
                      if (this.WindowState == FormWindowState.Minimized)
                          return;
                      if (lblctrl.InvokeRequired)
                          lblctrl.BeginInvoke(new SetLabel(setFilename), new object\[\] { lblctrl, filename });
                      else
                          lblctrl.Text = filename;
                  }
              
                  private void finalizeProcess()
                  {
                      //progressBar1.Style = ProgressBarStyle.Blocks;
                      //progressBar1.Value = 0;
                      //progressBar1.Dispose();
                      GC.Collect();
                      //base.Close();
                  }
              
                  #endregion
              
                  #region Open File Dialog Code
              
                  private void button3\_Click(object sender, EventArgs e)
                  {
                      if (openFileDialog1.ShowDialog() != DialogResult.OK)
                      {
                          return;
                      }
                      else
                      {
                          txtboxSelectFile.Text = openFileDialog1.FileName;
                      }
              
                  }
              
                  #endregion
              
                  #region Select File Code
              
                  private void btnStart\_Click(object sender, EventArgs e)
                  {
                      setText(this, "Checking Process Selection");
                      if (chkboxHolders.CheckState == CheckState.Unchecked && chkboxCert.CheckState == CheckState.Unchecked && 
                          chkboxPiadd.CheckState == CheckState.Unchecked && chkboxDivRecords.CheckState == CheckState.Unchecked)
                      {
              
              L Offline
              L Offline
              Luc Pattyn
              wrote on last edited by
              #6

              Hi, you are not supposed to touch a Control from a thread that did not create the Control. While your code is using several InvokeRequired/Invoke patterns, it is also not using them in several locations, see: 1. if (this.WindowState == FormWindowState.Minimized) is probably OK, but not documented to be OK 2. infile = txtboxSelectFile.Text; is probably not OK (not documented, I guess it is not OK for Controls that allow text editing) 3. outfile = saveFileDialog1.FileName; is most likely not OK, file dialogs are known to be critical (e.g. you need STAThread for them) IMO you should fix all of the above. :)

              Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


              I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
              All Toronto weekends should be extremely wet until we get it automated in regular forums, not just QA.


              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