473,702 Members | 2,679 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 changeProgressB arThread()
{
changeProgressB ar();
}

private void changeProgressB ar()
{
toolStripProgre ssBar1.Maximum = 100;
for (int i = 0; i <= 100; i++)
{
toolStripProgre ssBar1.Value = i;
if (i == 100)
{
for (int j = 100; j >= 0; j++)
{
toolStripProgre ssBar1.Value = j;
}
i = 0;
}
}
}
private void button1_Click(o bject sender, EventArgs e)
{

ThreadStart thrdDeleg1 = new ThreadStart(add DataSetThread);
Thread addDSThrd1 = new Thread(thrdDele g1);

ThreadStart thrdDeleg2 = new ThreadStart(cha ngeProgressBarT hread);
Thread addDSThrd2 = new Thread(thrdDele g2);

addDSThrd1.Star t();
addDSThrd2.Star t();

while (addDSThrd1.IsA live)
{
if(!addDSThrd1. IsAlive)
addDSThrd2.Abor t();
}
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 toolStripProgre ssBar1 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.IsA live)
{
if(!addDSThrd1. IsAlive)
addDSThrd2.Abor t();
}
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 34645
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 changeProgressB ar method
instead of calling it directly.

// declare delegate signature
private delegate void ChangeProgressB arCallback();

private void changeProgressB ar()
{
if(toolStripPro gressBar1.Invok eRequired)
{
// instantiate the callback instance out of this very method
ChangeProgressB arCallback callback = new ChangeProgressB arCallback(chan geProgressBar);

// invoke it, when it comes to it again InvokeRequired will be false
Invoke(callback );
}
else
{
toolStripProgre ssBar1.Maximum = 100;
for (int i = 0; i <= 100; i++)
{
toolStripProgre ssBar1.Value = i;
if (i == 100)
{
for (int j = 100; j >= 0; j++)
{
toolStripProgre ssBar1.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 changeProgressB arThread()
{
changeProgressB ar();
}

private void changeProgressB ar()
{
toolStripProgre ssBar1.Maximum = 100;
for (int i = 0; i <= 100; i++)
{
toolStripProgre ssBar1.Value = i;
if (i == 100)
{
for (int j = 100; j >= 0; j++)
{
toolStripProgre ssBar1.Value = j;
}
i = 0;
}
}
}
private void button1_Click(o bject sender, EventArgs e)
{

ThreadStart thrdDeleg1 = new ThreadStart(add DataSetThread);
Thread addDSThrd1 = new Thread(thrdDele g1);

ThreadStart thrdDeleg2 = new ThreadStart(cha ngeProgressBarT hread);
Thread addDSThrd2 = new Thread(thrdDele g2);

addDSThrd1.Star t();
addDSThrd2.Star t();

while (addDSThrd1.IsA live)
{
if(!addDSThrd1. IsAlive)
addDSThrd2.Abor t();
}
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 toolStripProgre ssBar1 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.IsA live)
{
if(!addDSThrd1. IsAlive)
addDSThrd2.Abor t();
}
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 changeProgressB ar method
instead of calling it directly.
[...]
if(toolStripPro gressBar1.Invok eRequired)
The ToolStripProgre ssBar 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 changeProgressB ar method
instead of calling it directly.
[...]
if(toolStripPro gressBar1.Invok eRequired)

The ToolStripProgre ssBar 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
5511
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
19620
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
4776
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 continually update prgValue (and display its progress), and then close frmProgress upon completion? ...
3
4790
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 affordable billing solution
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 contain the same amount of data. I solved this with starting an other thread to increment the...
0
1550
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 ProgressBar.Value is changed. Me.Refresh(), Me.Invalidate() or Me.Update() and Appliocation.DoEvents()...
2
11116
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 text I want such as "Updating data..." and then set the ProgressBar's Style property to Marquee...
9
7292
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 game where you the "hero" meets "villains" and monsters in an arena. I want the fighting to be like...
6
10701
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 progress bar
0
8739
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8652
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9234
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9089
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7831
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6575
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5907
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4667
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2402
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.