Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / November 2006

Tip: Looking for answers? Try searching our database.

Changing ProgressBar in a seperate Thread

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Martin Pöpping - 20 Nov 2006 21:13 GMT
Hello,

I want to change a ProgressBar in a separate Thread.

My current code is the following:

private void changeProgressBarThread()
{
    changeProgressBar();
}

private void changeProgressBar()
{
    toolStripProgressBar1.Maximum = 100;
    for (int i = 0; i <= 100; i++)
    {
       toolStripProgressBar1.Value = i;
           if (i == 100)
              {
               for (int j = 100; j >= 0; j++)
               {
                  toolStripProgressBar1.Value = j;
               }
              i = 0;
           }
       }
}

private void button1_Click(object sender, EventArgs e)
{

    ThreadStart thrdDeleg1 = new ThreadStart(addDataSetThread);
    Thread addDSThrd1 = new Thread(thrdDeleg1);

    ThreadStart thrdDeleg2 = new ThreadStart(changeProgressBarThread);
    Thread addDSThrd2 = new Thread(thrdDeleg2);

    addDSThrd1.Start();
    addDSThrd2.Start();

    while (addDSThrd1.IsAlive)
    {
    if(!addDSThrd1.IsAlive)
            addDSThrd2.Abort();
    }
    addDSThrd1.Join();

    string[] row = new String[3];
    row[0] = searchText_;
    row[1] = results_.count.ToString();
    row[2] = "0";
    dgViewData.Rows.Add(row);

}

My problem is now, that I cannot use the toolStripProgressBar1 object:

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

I understand the error message but... How can I fix the error?
I do not know how to update my progress bar if I cannot update it in my
seperate thread.

How do you do that?

It would be nice if you could give me a small example.

And my other question concerns the following code lines:

   while (addDSThrd1.IsAlive)
    {
    if(!addDSThrd1.IsAlive)
            addDSThrd2.Abort();
    }

I think these lines of code are not very well/ secure,
because it looks bit like an endless loop.

How do you let another thread (here my progress bar thread) stop if a
long calculation in another thread is finished?

Regards,

Martin
Sericinus hunter - 20 Nov 2006 21:34 GMT
Try to exploit the following idea. Check if Invoke is required
(this is the only thread-safe property of a control) and if it is,
Invoke a delegate initialized with your changeProgressBar method
instead of calling it directly.

// declare delegate signature
private delegate void ChangeProgressBarCallback();

private void changeProgressBar()
{
    if(toolStripProgressBar1.InvokeRequired)
    {
      // instantiate the callback instance out of this very method
      ChangeProgressBarCallback callback = new ChangeProgressBarCallback(changeProgressBar);

      // invoke it, when it comes to it again InvokeRequired will be false
      Invoke(callback);
    }
    else
    {
      toolStripProgressBar1.Maximum = 100;
      for (int i = 0; i <= 100; i++)
      {
        toolStripProgressBar1.Value = i;
        if (i == 100)
        {
          for (int j = 100; j >= 0; j++)
          {
            toolStripProgressBar1.Value = j;
          }
          i = 0;
        }
      }
    }
}

> Hello,
>
[quoted text clipped - 81 lines]
>
> Martin
Martin Pöpping - 20 Nov 2006 22:00 GMT
Thanks for your answer!

Sericinus hunter schrieb:
>    Try to exploit the following idea. Check if Invoke is required
> (this is the only thread-safe property of a control) and if it is,
> Invoke a delegate initialized with your changeProgressBar method
> instead of calling it directly.
> [...]
>     if(toolStripProgressBar1.InvokeRequired)

The ToolStripProgressBar does not has a property "InvokeRequired" :-(
Sericinus hunter - 20 Nov 2006 22:29 GMT
Ah, I did not realize that, since never used this control myself.
However, I found the suggestion somewhere on the web, that you
can check this property with any other control (you can probably
create one, hidden, just for this very purpose):

http://www.devnewsgroups.net/group/microsoft.public.dotnet.framework.windowsform
s/topic43420.aspx


> Thanks for your answer!
>
[quoted text clipped - 7 lines]
>
> The ToolStripProgressBar does not has a property "InvokeRequired" :-(

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.