473,587 Members | 2,494 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem with faking a progress bar / threading.

For an application I'm creating I want to create a 'fake' progress
bar.
By fake I mean a progress bar that looks like it's doing something but
actually isn't.

I know philosophically this isn't sound. But my little app is a 'fake'
app and is designed to look like another - hence this seeming crazy
situation of needing to fake a progess bar.

PROBLEM.
I can not figure out how to delay the execution of my code to pause
between updates of the progress bar value, I had thought i could do
something like this.

progressbar1.va lue = 10;
wait for 20 seconds....
progressbar1.va lue=50;
wait for 10 seconds....
progressbar1.va lue=90;

etc... The problem I have is that the only method i've managed to find
of pausing code execution is the thread.sleep method. However this
causes the form not to be displayed, from what i've gathered this is
because the part of the code that actually paints the form is
disrupted by the call to make the thread sleep.

My Attempted Solution.

I tried to create a new thread and put the code that deals with the
progress bar in that thread. This created an error about cross thread
referencing. I then disabled cross thread referencing using
Control.CheckFo rIllegalCrossTh readCalls = false; but it's still not
working.

Any ideas how to fix this.
Here is what I have so far.
=============== ======
using System;
using System.Collecti ons.Generic;
using System.Componen tModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows. Forms;

namespace Stealth
{
public partial class Form3 : Form
{

public Form3()
{
InitializeCompo nent();
Control.CheckFo rIllegalCrossTh readCalls = false;

}
private void myStartingMetho d()
{
//change value to 20.
progressBar1.Va lue = 20;
//pause
System.Threadin g.Thread.Sleep( 5000);
//change value to 90.
progressBar1.Va lue = 90;
}
private void Form3_Load(obje ct sender, EventArgs e)
{

InitializeCompo nent();

System.Threadin g.Thread myThread;
myThread = new System.Threadin g.Thread(new
System.Threadin g.ThreadStart(m yStartingMethod ));
myThread.Start( );

}
}
}
=============== ==
TIA.

Sep 20 '07 #1
5 6924
On 20 Sep, 13:02, CCLeasing <g...@ccleasing .co.ukwrote:
For an application I'm creating I want to create a 'fake' progress
bar.
By fake I mean a progress bar that looks like it's doing something but
actually isn't.

I know philosophically this isn't sound. But my little app is a 'fake'
app and is designed to look like another - hence this seeming crazy
situation of needing to fake a progess bar.

PROBLEM.
I can not figure out how to delay the execution of my code to pause
between updates of the progress bar value, I had thought i could do
something like this.

progressbar1.va lue = 10;
wait for 20 seconds....
progressbar1.va lue=50;
wait for 10 seconds....
progressbar1.va lue=90;

etc... The problem I have is that the only method i've managed to find
of pausing code execution is the thread.sleep method. However this
causes the form not to be displayed, from what i've gathered this is
because the part of the code that actually paints the form is
disrupted by the call to make the thread sleep.

My Attempted Solution.

I tried to create a new thread and put the code that deals with the
progress bar in that thread. This created an error about cross thread
referencing. I then disabled cross thread referencing using
Control.CheckFo rIllegalCrossTh readCalls = false; but it's still not
working.

Any ideas how to fix this.
Here is what I have so far.
=============== ======
using System;
using System.Collecti ons.Generic;
using System.Componen tModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows. Forms;

namespace Stealth
{
public partial class Form3 : Form
{

public Form3()
{
InitializeCompo nent();
Control.CheckFo rIllegalCrossTh readCalls = false;

}
private void myStartingMetho d()
{
//change value to 20.
progressBar1.Va lue = 20;
//pause
System.Threadin g.Thread.Sleep( 5000);
//change value to 90.
progressBar1.Va lue = 90;

}
private void Form3_Load(obje ct sender, EventArgs e)
{

InitializeCompo nent();

System.Threadin g.Thread myThread;
myThread = new System.Threadin g.Thread(new
System.Threadin g.ThreadStart(m yStartingMethod ));

myThread.Start( );

}

}}

=============== ==
TIA.
It doesn't solve your threading problem, but using a timer to
increment the progress bar will have the same desired effect:
add a progressbar, button & timer to your form and try the following
code to see how it might work:

private void timer1_Tick(obj ect sender, System.EventArg s e)
{
if (progressBar1.V alue < 100)
{
progressBar1.Va lue = progressBar1.Va lue + 5;
}
}

private void button1_Click(o bject sender, System.EventArg s e)
{
progressBar1.Va lue = 0;
}

private void Form1_Load(obje ct sender, System.EventArg s e)
{
timer1.Enabled = true;
}

Adjust the timer interval and the amount the progress bar increments
as needed.

Sep 20 '07 #2
In your myStartingMetho d method, look at using progressBar1.In voke()
or BeginInvoke(). These methods provide the functionality you need.

Sep 20 '07 #3
CCLeasing wrote:
For an application I'm creating I want to create a 'fake' progress
bar.
By fake I mean a progress bar that looks like it's doing something but
actually isn't.

I know philosophically this isn't sound. But my little app is a 'fake'
app and is designed to look like another - hence this seeming crazy
situation of needing to fake a progess bar.
Use a BackgroundWorke r and set it to report its progress. This is what
it is designed for.

Chris.
Sep 20 '07 #4
On Sep 20, 8:02 am, CCLeasing <g...@ccleasing .co.ukwrote:
For an application I'm creating I want to create a 'fake' progress
bar.
By fake I mean a progress bar that looks like it's doing something but
actually isn't.

I know philosophically this isn't sound. But my little app is a 'fake'
app and is designed to look like another - hence this seeming crazy
situation of needing to fake a progess bar.

PROBLEM.
I can not figure out how to delay the execution of my code to pause
between updates of the progress bar value, I had thought i could do
something like this.

progressbar1.va lue = 10;
wait for 20 seconds....
progressbar1.va lue=50;
wait for 10 seconds....
progressbar1.va lue=90;

etc... The problem I have is that the only method i've managed to find
of pausing code execution is the thread.sleep method. However this
causes the form not to be displayed, from what i've gathered this is
because the part of the code that actually paints the form is
disrupted by the call to make the thread sleep.

My Attempted Solution.

I tried to create a new thread and put the code that deals with the
progress bar in that thread. This created an error about cross thread
referencing. I then disabled cross thread referencing using
Control.CheckFo rIllegalCrossTh readCalls = false; but it's still not
working.

Any ideas how to fix this.
Here is what I have so far.
=============== ======
using System;
using System.Collecti ons.Generic;
using System.Componen tModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows. Forms;

namespace Stealth
{
public partial class Form3 : Form
{

public Form3()
{
InitializeCompo nent();
Control.CheckFo rIllegalCrossTh readCalls = false;

}
private void myStartingMetho d()
{
//change value to 20.
progressBar1.Va lue = 20;
//pause
System.Threadin g.Thread.Sleep( 5000);
//change value to 90.
progressBar1.Va lue = 90;

}
private void Form3_Load(obje ct sender, EventArgs e)
{

InitializeCompo nent();

System.Threadin g.Thread myThread;
myThread = new System.Threadin g.Thread(new
System.Threadin g.ThreadStart(m yStartingMethod ));

myThread.Start( );

}

}}

=============== ==
TIA.
Calls that update a UI object have to be marshaled onto the UI
thread. I find the following pattern helpful:

void MethodThatUpdat esUI()
{
if (InvokeRequired )
Invoke(new MethodInvoker(M ethodThatUpdate sUI));
else
{
// update whatever ui controls
}
}

The only downside is the proliferation of one-off delegates if you
need to pass data into/out of the method.

Sep 20 '07 #5
Arnshea wrote:
Calls that update a UI object have to be marshaled onto the UI
thread. I find the following pattern helpful:

void MethodThatUpdat esUI()
{
if (InvokeRequired )
Invoke(new MethodInvoker(M ethodThatUpdate sUI));
else
{
// update whatever ui controls
}
}

The only downside is the proliferation of one-off delegates if you
need to pass data into/out of the method.
That latter issue is not a problem at all with the use of anonymous methods.

However, I don't like the general pattern myself, because of the
proliferation of methods that do two completely different things
depending on what thread they are on.

I prefer to either have a single method always used regardless of thread
and which always calls Invoke(), or to simply have the thread be aware
of whether it needs to call Invoke() itself.

That said, IMHO Matt's answer was the best in this situation, since no
actual work is being done in a background thread. All that's really
necessary is to update the progress bar periodically, and a Timer is a
fine way to do that without introducing any of the cross-thread issues
while still preserving a responsive UI.

Pete
Sep 21 '07 #6

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

Similar topics

13
3613
by: Jason Jacob | last post by:
To all, I have a GUI program (use c#), and I have create a Thread for loading some bulk data, I also arrange the GUI program like this: 1) load a form showing "Wait for loading..." etc 2) a Thread is then created to load the bulk data 3) after the thread has completed, close the "Wait for loading" form 4) show the main form for the GUI program
9
13601
by: Popoxinhxan | last post by:
Dear experts, i want to develop an client application that consume the google search web service. In my MainForm i have a method to retrieve all the search result e.g. GetGoogleResults(). Now i want to have a nice progress bar in another form e.g ProgressForm to perform the search action. i tried to do it but the progress bar won't work and the GUI freezed. I knew how to do with the single threading but i don't know how to do it in...
3
3684
by: | last post by:
I am working on a web/webservice application that has a service layer. Most service methods will perform an access-check before executing. This check uses the IPrincipal credentials available in HttpContext.Current. This works great. My issue is that I need to write unit tests for those service methods. These tests then run outside of IIS, thus there is no HttpContext.Current available - and the servicemethod will not execute. The two...
18
1783
by: Derek Martin | last post by:
Hi there, this is probably really dumb, but I am using a dialog as my main form and my startup form is my splash screen - which implements some progress bars to load up some relevant data. When that is done, loading form does this: Me.Visible = False mainform.ShowDialog() Application.Exit() So far so good (is this a dumb way of doing that???). Now, I am
9
1893
by: JoeB | last post by:
Hi, This is 100% reproducable. .NetCF / WinCE5.0 / VS.Net 2003 1. Create a new 'smart device application' project. 2. Add a progress bar to the form 'progressBar1' 3..Add: using System.Threading; to the top of the code.
9
3260
by: esakal | last post by:
Hello, I'm programming an application based on CAB infrastructure in the client side (c# .net 2005) Since my application must be sequencally, i wrote all the code in the UI thread. my problem occurs when i try to show a progress bar. The screen freezes. I know i'm not the first one to ask about it. but i'm looking
3
8647
by: Ritesh Raj Sarraf | last post by:
Hi, I have a small application, written in Python, that uses threads. The application uses function foo() to download files from the web. As it reads data from the web server, it runs a progress bar by calling an install of a progress bar class. When using threads, I get the problem that the progress bar gets over-written by the download progress of files from other threads.
1
4105
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 framework 1.1). The application implements a Progress Bar webcontrol, that pops up in a window, using the HttpHandler interface, on the event of a button click. The handler's process request routine reads the status of the progress by querying the...
5
10885
by: Miro | last post by:
I will try my best to ask this question correctly. I think in the end the code will make more sence of what I am trying to accomplish. I am just not sure of what to search for on the net. I have a form that has a button. ( this form is a child form of a parent form ( main form ). Anway...in this child form I have a button, and if clicked a bunch of code will get executed. I would like to show a Progress Bar / form in modal/ShowDialog...
0
7924
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
8349
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
8221
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6629
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
5722
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
5395
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
3845
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3882
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2364
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.