Using toolStripProgressBar1 inside a Thread

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gabielmatos
    New Member
    • Oct 2009
    • 25

    Using toolStripProgressBar1 inside a Thread

    I recive the following error:
    Cross-thread operation not valid: Control '' accessed from a thread other than the thread it was created on.

    this is my code:
    Code:
    ParameterizedThreadStart Proceso = new ParameterizedThreadStart(this.ReadNPI);
                start2 = new Thread(Proceso);
                start2.IsBackground = true;
                start2.Priority = ThreadPriority.Highest;            
                start2.Start(FilePath2);
    Code:
     private void ReadNPI(object FilePath)
            {            
                string FILE = (string)FilePath;
                string connectionString = cnSelected;
                DataSet dataSet1 = new DataSet();
                try
                {
                    string selectString = "SELECT * FROM [" + sheet + "$]";
                    OleDbConnection con = new OleDbConnection(connectionString);
                    OleDbCommand cmd = new OleDbCommand(selectString, con);
                    con.Open();
                    OleDbDataReader theData = cmd.ExecuteReader();
                    List<string> al = new List<string>();
                    int c = theData.FieldCount; 
                    while (theData.Read())
                    {   
                        string NPI = theData.GetValue(0).ToString();
                        al.Add(NPI);             //al es un ArrayList Publico.);
                    }
                    int count = al.Count;
                    this.toolStripProgressBar1.Minimum = 0;
                    this.toolStripProgressBar1.Maximum = count; // The error is HERE
                    this.toolStripProgressBar1.Step = 1;
                    con.Close();                
                    try
                    {
    
                        string filepath = (string)FilePath;  // FilePath es el archivo de NPPES.
                    Begin:
                        StreamReader sr = new StreamReader(filepath);
                        while (sr.Peek() != -1)
                        {
                        Back:
                            string line = sr.ReadLine();
                            string skip = line.Substring(1, 3);
                            if (skip == "NPI")
                            {
                                goto Back;
                            }
                            string[] fields = Regex.Split(line, "\",\"");
                            string NPI = fields.GetValue(0).ToString().Trim().Replace("\"", "");
                            if (al.Contains(NPI))
                            {
                                InsertData(fields);
                                al.Remove(NPI);
                                sr.Close();
                                sr.Dispose();
                                this.toolStripProgressBar1.PerformStep();
                                if (al.Count == 0)
                                {
                                    toolStripStopThread.Visible = false;
                                    TerminateThreads();
                                    break;
                                }
                                goto Begin;
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        TerminateThreads();
                        return;
                    }            
    
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    TerminateThreads();
                    return;
                }       
            }
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    Originally posted by OriginalPoster
    Original Poster: I have xxx, then do yyy then zzz happens. Here's my code ...
    Ok. You know what you have.
    What you don't have is a question. Nobody here knows why you posted this since you haven't asked anything. You could be asking "What does the error mean?", you could be asking "Which control is it talking about?", you could be asking "Will someone do my debugging for me?"

    I recommend you read the FAQ about How to ask a good question so the volunteers will be able to help you.

    Commonly this comes from creating a new thread, that then tries to make a change to control on form that was made from the primary thread of the application. For example, your main form has a label or textbox (or progressbar in your case) where you are putting up status updates... and your worker thread is trying to directly write those updates directly to the control. You can't do that. You would need the worker thread to raise an event that your other thread would subscribe to, then react to.

    This might help. Two forms, two classes, two threads... its all basically the same thing.

    Originally posted by OriginalPoster
    Original Poster: How do I get my Form2 to react to something on my Form1?
    How do I make my Form1 control something on my Form2?
    Although you can have Form1 directly access items on Form2 it isn't the recommended way to go. It ties the two forms tightly to each other. If a change is made in Form2 such as removing one of the controls, then code in Form1 breaks.
    It is better to Form1 raise an event, and have Form2 contain its own code for how to react to this. This places responsibility for Form2 within Form2, and Form1 within Form1.
    It keeps Form1 blissfully ignorant of Form2 - and a logging component - and a progress component, and a dozen other little black boxes that can be subscribed to events in Form1, all without Form1 being made responsible for directly affecting controls other than itself.
    Events tutorial (including Form to Form which is the same as class to class)
    This tutorial for a cash register does exactly that: It makes a virtual numeric keyboard.

    Bad: Directly accessing controls of one class/form from another.
    Code:
    bool IsOn = Form2.button1.IsChecked;
    Good: Use a property to get such information
    Code:
    //Form1
    bool IsOn = Form2.IsOn;
    Code:
    //Form 2
    public bool IsOn
    {
       get { return CheckBox1.Checked; }
       set { CheckBox1.Checked = value; }
    }
    It's a subtle but important difference as your applications become more complex. Using properties means your target class/form (Form2) can be changed and updated a thousand different ways yet won't negatively impact the classes that are reading from it. If you change the name of a control for example: It won't break references in all your other classes. If your target form evolves where it needs to do 10 things when it is turned on, then the responsibility stays within Form2, and that burden is not put on Form1 and a dozen other forms that might be using Form2. The goal is to compartimentali ze the work so that Form2 is responsiblity SOLELY for Form2. From1 should only have to say "Turn on" or "Turn Off"

    Form2
    Code:
    public bool IsOn
    {
       get { return btnMeaningfulName.Checked; }
       set {
                btnMeaningfulName.Checked = value;
                panelDashboard.Visible = value;
                labelStatus = value == true ? "On" : "Off";
                btnRunNow.Enabled = value;
           }
    }
    Now when Form1 tells Form2 to turn on, Form2 will check a box, make an entire panel visible, change its Status label to say 'On' and enable a Run Now button.

    Comment

    • gabielmatos
      New Member
      • Oct 2009
      • 25

      #3
      My question is;
      How use toolStripProgre ssBar1 inside a Thread?
      Because I recive the following error in this line:
      Cross-thread operation not valid: Control '' accessed from a thread other than the thread it was created on.

      Code:
       this.toolStripProgressBar1.Maximum = count;
      Starting the Thread:
      Code:
      ParameterizedThreadStart Proceso = new ParameterizedThreadStart(this.ReadNPI);
                  start2 = new Thread(Proceso);
                  start2.IsBackground = true;
                  start2.Priority = ThreadPriority.Highest;            
                  start2.Start(FilePath2);
      Code:
       private void ReadNPI(object FilePath)
              {            
                  string FILE = (string)FilePath;
                  string connectionString = cnSelected;
                  DataSet dataSet1 = new DataSet();
                  try
                  {
                      string selectString = "SELECT * FROM [" + sheet + "$]";
                      OleDbConnection con = new OleDbConnection(connectionString);
                      OleDbCommand cmd = new OleDbCommand(selectString, con);
                      con.Open();
                      OleDbDataReader theData = cmd.ExecuteReader();
                      List<string> al = new List<string>();
                      int c = theData.FieldCount; 
                      while (theData.Read())
                      {   
                          string NPI = theData.GetValue(0).ToString();
                          al.Add(NPI);             //al es un ArrayList Publico.);
                      }
                      int count = al.Count;
                      this.toolStripProgressBar1.Minimum = 0;
                      this.toolStripProgressBar1.Maximum = count;
                      this.toolStripProgressBar1.Step = 1;
                      con.Close();               
                       try
                      {
      
                          string filepath = (string)FilePath;  // FilePath es el archivo de NPPES.
                      Begin:
                          StreamReader sr = new StreamReader(filepath);
                          while (sr.Peek() != -1)
                          {
                          Back:
                              string line = sr.ReadLine();
                              string skip = line.Substring(1, 3);
                              if (skip == "NPI")
                              {
                                  goto Back;
                              }
                              string[] fields = Regex.Split(line, "\",\"");
                              string NPI = fields.GetValue(0).ToString().Trim().Replace("\"", "");
                              if (al.Contains(NPI))
                              {
                                  InsertData(fields);
                                  al.Remove(NPI);
                                  sr.Close();
                                  sr.Dispose();
                                  this.toolStripProgressBar1.PerformStep();
                                  if (al.Count == 0)
                                  {
                                      toolStripStopThread.Visible = false;
                                      TerminateThreads();
                                      break;
                                  }
                                  goto Begin;
                              }
                          }
                      }
                      catch (Exception ex)
                      {
                          MessageBox.Show(ex.Message, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
                          TerminateThreads();
                          return;
                      }            
      
                  }
                  catch (Exception ex)
                  {
                      MessageBox.Show(ex.Message);
                      TerminateThreads();
                      return;
                  }       
              }

      Comment

      • tlhintoq
        Recognized Expert Specialist
        • Mar 2008
        • 3532

        #4
        Gabiel: How use toolStripProgre ssBar1 inside a Thread?
        If the toolStripProgre ssBar was made from a different thread (the main thread that made the form) then you don't. That's what the error message is telling you. One thread does not have the ability to affect controls made from a different thread. Your progressbar was made by the thread that build the form. Your ReadNPI method is happening on a different thread.

        Your ReadNPI thread Processo needs to raise an event, and your main GUI thread needs to react to that event.

        Comment

        • Christian Binder
          Recognized Expert New Member
          • Jan 2008
          • 218

          #5
          You have to use Form's Invoke()-method, this would marshal your operation to the GUI-thread. Because it's not allowed, to do something in your GUI from another thread, which the GUI (Control-object) was created.

          You have to do this everytime you set a control's property or call it's method.

          E.g. instead of
          Code:
          this.toolStripProgressBar1.Minimum = 0;
          this.toolStripProgressBar1.Maximum = count;
          this.toolStripProgressBar1.Step = 1;
          you'd better use (if your'e using .net 3.5)

          Code:
          this.Invoke(new Action(() => {
            this.toolStripProgressBar1.Minimum = 0;
            this.toolStripProgressBar1.Maximum = count;
            this.toolStripProgressBar1.Step = 1;
          }
          Invoke() is a synchronous (blocking) call, if you want to do a non-blocking call, you could use Control's BeginInvoke()-method (has the same signature as Invoke() )

          Comment

          • tlhintoq
            Recognized Expert Specialist
            • Mar 2008
            • 3532

            #6
            Yeah, you can use invoke... but I would avoid it. It tightly ties your thread/class to the GUI. If you change the GUI then you break the other threads. It really violates the whole idea that each part of your application should be a self-contained little black box that does its own job and nobody else's.

            If you have your thread raise an event... You could have 10 different classes subscribe to it. Or none. One could update the progress bar, one could add a log entry, one could add the file to a processing queue, one could open another form asking for feedback on how the user liked the system....

            The thread should only be responsible for what the thread does - not the GUI user experience.

            Comment

            • Christian Binder
              Recognized Expert New Member
              • Jan 2008
              • 218

              #7
              Originally posted by tlhintoq
              Yeah, you can use invoke... but I would avoid it. It tightly ties your thread/class to the GUI. If you change the GUI then you break the other threads. It really violates the whole idea that each part of your application should be a self-contained little black box that does its own job and nobody else's.

              If you have your thread raise an event... You could have 10 different classes subscribe to it. Or none. One could update the progress bar, one could add a log entry, one could add the file to a processing queue, one could open another form asking for feedback on how the user liked the system....

              The thread should only be responsible for what the thread does - not the GUI user experience.
              Yes, it's true, the Thread's operations shouldn't be responsible for how the GUI should handle this.
              But in the source-code the Thread is private within the Form, so there won't be problems in case of code reusage or anything else.
              If the threaded operation would be done outside (other class than) the form-class, it wouldn't be nice to do this Invoke.

              But at least you'd nee to do invocation anyway, because if you'd use an event in the Tread, your event-handler would be called from the Thread's context and invocation will be necessary to do GUI things.
              Now there are different approaches, how to handle it, the simplest would be
              (it's conceptual, I don't know, if it's working that way)
              Code:
              private void Thread_Ready(object sender, EventArgs e) {
                if(InvokeRequired)
                  Invoke(new Action(DoSomething));
                else
                  DoSomething()
              }
              
              private void DoSomething() { ... }
              the other approach would be marshalling in your threaded context
              e.g.
              Code:
              class MyThreadedOperation {
                List<KeyValuePair<Action, SynchronizationContext>> _callbacks = new List...();
              
                public event Action OnReady {
                  add {
                    _callbacks.Add(new KeyValuePair(value, SynchronizationContext.Current ?? new SynchronizationContext());
                  }
              
                  remove {
                    _callbacks.Remove(value); //do check if list contains before ...
                  }
                }
              
                public void DoSomethingThreaded() {
                  new Thread(...(DoSomething)).Start();
                }
              
                private void DoSomething() {
                  // do some heavy operations
              
                  // callback listeners
                  foreach(KeyValuePair<Action, SynchronizationContext> kvp in _callbacks) {
                    kvp.Value.Post(new SendOrPostCallback(foo => kvp.Key()), null);
                    // Post-method is async like BeginInvoke()
                    // Send-methood would be synchronous like Invoke()
                  }
                }
              }

              Comment

              Working...