473,520 Members | 2,562 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Changing ProgressBar in a seperate Thread

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
Nov 20 '06 #1
3 34623
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;
}
}
}
}
Martin Pöpping wrote:
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
Nov 20 '06 #2
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" :-(
Nov 20 '06 #3
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/m...opic43420.aspx

Martin Pöpping wrote:
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" :-(
Nov 20 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
5502
by: | last post by:
Hi all, continued from yesterday's posting... I still haven't found a solution to this issue. I put a breakpoint in private void SqlBackupPercentComplete(string message, int Percent) { progressBar1.Value = Percent; progressBar1.Update(); }
8
19584
by: needin4mation | last post by:
Please consider: foreach (ListViewItem item in listViewFiles.Items) { // Display the ProgressBar control. pBar1.Visible = true; // Set Minimum to 1 to represent the first file being copied. pBar1.Minimum = 1; // Set Maximum to the total number of files to copy. pBar1.Maximum = 4; //filenames.Length;
4
4765
by: pmcguire | last post by:
Rather than putting a progress bar on all of my forms to show progress during time consuming tasks, I made a form called frmProgress with 2 controls, a Label and a ProgressBar. Suppose I expose 1 property -- prgValue (the Value of the ProgressBar); how do I use threading so that my time consuming task will open and display frmProgress as well as...
3
4768
by: Mitchell Vincent | last post by:
In other programming languages I've been able to easily change the style of a progress bar between smooth and blocked. I find that is either really hidden or impossible in .NET. Am I missing something? I'm shocked that .NET doesn't have a style property for the progressbar control! -- - Mitchell Vincent - kBilling - An easy and...
1
696
by: nobody | last post by:
Hi I'm currently developing a Windows application. At the start of the application I load several tables into datatables in a dataset. I also use a progressbar to show the user how much percent of the total is already loaded. But I do this in a dirty way: I increment the progressbar with the same number after each table, but not all tables...
0
1542
by: Rick | last post by:
I use VB .Net and try to do som intensive actions on a database (in a seperate module). I defined an Interface for updating a progressbar on the main form. When I test it from a procedure within the Main Form Class, all works Ok. But when I use the Interface functions, the controls (ProgressBar, Textboxes) are not updated, although the...
2
11098
by: =?Utf-8?B?QWFyb24=?= | last post by:
Since some controls such as the DataGridView take a long time to update themselves when performing certain tasks, I have added a StatusStrip with a ProgressBar on it. While I am updating the controls on the form, I want the ProgressBar to scroll in marquee mode. However, I cannot seem to get this to work. I set the StatusStripLabel to the...
9
7279
by: appelsinagurk | last post by:
Hi I'm fairly new to .Net programming so I'll try to explain my problem as easy as I can, and in advanced sorry for my poor english. I've got some spare hours where I work, so I've decided to spend them learning C# .Net. I've done some smaller programming assignments and decided now to try a larger program. I'm trying to create a basic...
6
10673
by: JB | last post by:
Hi All, I have a Windows Form application that shows a "please wait" form while I query a database for various information. I display my form using Show, then run my DB query, then Close the form at the end. To make it look even nicer I placed a marquee style Progress Bar on my form. It works exactly the same way, but the marquee style...
0
7243
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7471
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7632
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7595
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5771
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5162
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4809
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3298
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
533
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.