473,394 Members | 1,663 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Progress Bar refuses to work?

Hello all, Ive been trying to get a helpful progress bar at the bottom
of my app to simply fill up and then disapear.

Unfortunatly, i get this error message when i try to change the .Value
field of the progress bar: "A first chance exception of type
'System.InvalidOperationException' occurred in
System.Windows.Forms.dll" which may be THE most unhelpful error
message ever.

Here is the code:

private void startNetworkProgressBar()
{
tslReadingFromNetwork.Visible = true;
progressBar.Visible = true;
progressBar.Maximum = 100;
progressBar.Minimum = 0;
progressBar.Step = 10;

progressTimer = new System.Timers.Timer();
progressTimer.Interval = 1000;
progressTimer.Elapsed += new
System.Timers.ElapsedEventHandler(progressTimer_El apsed);
progressTimer.Start();
}

void progressTimer_Elapsed(object sender,
System.Timers.ElapsedEventArgs e)
{
if (progressBar.Value == 100)
{
tslReadingFromNetwork.Visible = false;
progressBar.Visible = false;
progressTimer.Stop();
}
else
{
progressBar.Value += 10 ;
}
}

Can anyone see why this isnt working? (Ive tried changing the
progressBar.Value +=10 to .performStep() and that gives the same error.

Apr 5 '07 #1
4 6892
gw********@gmail.com wrote:
Hello all, Ive been trying to get a helpful progress bar at the bottom
of my app to simply fill up and then disapear.

Unfortunatly, i get this error message when i try to change the .Value
field of the progress bar: "A first chance exception of type
'System.InvalidOperationException' occurred in
System.Windows.Forms.dll" which may be THE most unhelpful error
message ever.

Here is the code:

private void startNetworkProgressBar()
{
tslReadingFromNetwork.Visible = true;
progressBar.Visible = true;
progressBar.Maximum = 100;
progressBar.Minimum = 0;
progressBar.Step = 10;

progressTimer = new System.Timers.Timer();
progressTimer.Interval = 1000;
progressTimer.Elapsed += new
System.Timers.ElapsedEventHandler(progressTimer_El apsed);
progressTimer.Start();
}

void progressTimer_Elapsed(object sender,
System.Timers.ElapsedEventArgs e)
{
if (progressBar.Value == 100)
{
tslReadingFromNetwork.Visible = false;
progressBar.Visible = false;
progressTimer.Stop();
}
else
{
progressBar.Value += 10 ;
}
}

Can anyone see why this isnt working? (Ive tried changing the
progressBar.Value +=10 to .performStep() and that gives the same error.
The Timer is running on a different thread, so when the event handler
runs, it is trying to access progressBar from a thread other than that
it was created on, which is a no-no for UI elements. Read
<http://www.skeet.org.uk/csharp/threads/winforms.shtml& surrounding
pages for the full story; or for a quick 'don't bother me with details'
fix, change your event handler like so:

void progressTimer_Elapsed(object sender,
System.Timers.ElapsedEventArgs e)
{
this.Invoke((MethodInvoker)delegate
{
if (progressBar.Value == 100)
{
//tslReadingFromNetwork.Visible = false;
progressBar.Visible = false;
progressTimer.Stop();
}
else
{
progressBar.Value += 10;
}
});

All I have done here is wrap your existing code in an anonymous
delegate, which is cast to MethodInvoker (a delegate type for functions
from void to void), and then invoked on the form's thread by the
this.Invoke. This approach requires C# 2.0.

--
Larry Lard
la*******@googlemail.com
The address is real, but unread - please reply to the group
For VB and C# questions - tell us which version
Apr 5 '07 #2
Larry,

GREAT tip with the annonymous delegate. That is deffinatly going to be
used in my code:

But my code already invoked the method: Here is the proceeding call:
... [Method on a different thread]..
doubleVoidDelegate doubleVoid= new
doubleVoidDelegate(startNetworkProgressBar);
try { Invoke(doubleVoid); }
catch { }

// Send conscription call.
udpclient.Send(datagram,
datagram.Length);
}
}

private void startNetworkProgressBar()
{

So the methods are already running from an invoke method, and besides
that, the method is allowing me to make the Controls visible and
invisible. Am i missing something (very likley).
Apr 5 '07 #3
Using your code does fix the problem though..

The timer is called in my main method, but the timer event is starting
in a different thread...

I don't suppose you could explain why the timer is doing this?

Graeme
PS. Threads confuse me. :)
Apr 5 '07 #4
On Apr 5, 12:15 pm, Larry Lard <larryl...@googlemail.comwrote:
gwoodho...@gmail.com wrote:
Hello all, Ive been trying to get a helpful progress bar at the bottom
of my app to simply fill up and then disapear.
Unfortunatly, i get this error message when i try to change the .Value
field of the progress bar: "A first chance exception of type
'System.InvalidOperationException' occurred in
System.Windows.Forms.dll" which may be THE most unhelpful error
message ever.
Here is the code:
private void startNetworkProgressBar()
{
tslReadingFromNetwork.Visible = true;
progressBar.Visible = true;
progressBar.Maximum = 100;
progressBar.Minimum = 0;
progressBar.Step = 10;
progressTimer = new System.Timers.Timer();
progressTimer.Interval = 1000;
progressTimer.Elapsed += new
System.Timers.ElapsedEventHandler(progressTimer_El apsed);
progressTimer.Start();
}
void progressTimer_Elapsed(object sender,
System.Timers.ElapsedEventArgs e)
{
if (progressBar.Value == 100)
{
tslReadingFromNetwork.Visible = false;
progressBar.Visible = false;
progressTimer.Stop();
}
else
{
progressBar.Value += 10 ;
}
}
Can anyone see why this isnt working? (Ive tried changing the
progressBar.Value +=10 to .performStep() and that gives the same error.

The Timer is running on a different thread, so when the event handler
runs, it is trying to access progressBar from a thread other than that
it was created on, which is a no-no for UI elements. Read
<http://www.skeet.org.uk/csharp/threads/winforms.shtml& surrounding
pages for the full story; or for a quick 'don't bother me with details'
fix, change your event handler like so:

void progressTimer_Elapsed(object sender,
System.Timers.ElapsedEventArgs e)
{
this.Invoke((MethodInvoker)delegate
{
if (progressBar.Value == 100)
{
//tslReadingFromNetwork.Visible = false;
progressBar.Visible = false;
progressTimer.Stop();
}
else
{
progressBar.Value += 10;
}
});

All I have done here is wrap your existing code in an anonymous
delegate, which is cast to MethodInvoker (a delegate type for functions
from void to void), and then invoked on the form's thread by the
this.Invoke. This approach requires C# 2.0.

--
Larry Lard
larryl...@googlemail.com
The address is real, but unread - please reply to the group
For VB and C# questions - tell us which version- Hide quoted text -

- Show quoted text -
It's better to avoid Invoke() and to use BeginInvoke() instead.

For more explanation see here: http://kristofverbiest.blogspot.com/...gininvoke.html

Apr 6 '07 #5

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

Similar topics

16
by: Paul | last post by:
i have been working with vb6 for a while but never had the pleasure of using progress bars. That is until now, one of the programs i have written has just been modified so that large csv files of...
5
by: James Morton | last post by:
I have a Static DataSet in a class file that I am using globally between a few forms. The main form populates the dataset through a menu option which invokes ReadXML in the class file to populate...
3
by: Tim::.. | last post by:
Can someone please, please tell me how I get a progress bar to work with a binary file upload! I have the following code that uploads files into an SQL Database using a Stored Procedure but I...
8
by: WhiteWizard | last post by:
I guess it's my turn to ASK a question ;) Briefly my problem: I am developing a Windows app that has several User Controls. On one of these controls, I am copying/processing some rather large...
1
by: daniel_xi | last post by:
Hi all, I am running a VS 2003 .NET project on my client machine (Win 2000 SP4, ..NET framework 1.1), running an ASP.NET application on a remote web server (Win 2000 Server, IIS 6.0, .NET...
10
by: Robertf987 | last post by:
Okay, now then. I'm hoping somebody can help here, pretty please. I want to make a progress bar/indicator on a form. At first I was just going to insert an animated gif, but I've tried and remembered...
2
by: mcw.willart | last post by:
Hi, I use a backgroundworker to get the total size of a homeshare (as it is a bit time-consuming). Wat i would like to do, is show the progress, but at start i don't know how much files/folders...
1
by: spinoza1111 | last post by:
I want the GUI of the spinoza system to not piss me off with the usual type of progress reporting one sees: the flashy, colorful, and utterly uninformative gizmos that go back and forth and round...
1
by: edinwoking | last post by:
Hi I have been struggling to get a toolbar to update its progress information from an event raised ina parallel for loop. I have tried using delegates to get it to work but it just hangs after...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...

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.