hi
thanks for your reply is the timer not running in a seperate thread anyway?
sorry can you elaborate on what you mean please? do i have to create a specific thread for the timer?
many thanks,
Matt.
Main Topics
Browse All Topicshi all,
i've added a progress bar to a form. no i have a static class for handling all my sql login etc.
in the sql login method i add a time out of 24 seconds, now i want to show progress (rather than appering to hang) for my program logging in.
i have created a timer for handling the event, and it is working however it doesnt seem to be working in the seperate thread.
the code seems to hang and then after the time out accurs shoots up. am i doing somehting wrong?
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
>>>> is the timer not running in a seperate thread anyway?
>>>> sorry can you elaborate on what you mean please? do i have to create a specific thread for the timer?
I have no experience with managed C++ or C# but perhaps I can put some light to the timers and the threading.
Your dialog runs in that what junges called the UI thread. Normally, it is the main and only thread. That UI thread permanently is running in a loop which looks like (pseudo code)
while (PeekMessage(mainWnd, Message))
{
GetMessage(mainWnd, Message);
TranslateMessage(mainWnd, Message);
DispatchMessage(mainWnd, Message);
}
In the DispatchMessage all handlers will be called that are provided by your classes or by system classes . Those handlers will post (set at end) or send (set at front) new messages to this so-called message pump, what keeps the system running. The loop ends when a WM_QUIT message was processed and then the UI thread (and normally) the process could end as well.
Given that message loop, there are two ways in Windows how to implement a timer.
(1) Performing a timer in the UI thread by using the message loop
Here no further thread is involved but handled by the message queue itself. After a timer was installed a WM_TIMER message was fired when the time has expired. You handle the timer message in a handler function like OnTimer.
Note, the WM_TIMER is a low priority message. It only was retrieved (and dispatched) in message queue if not higher priority messages were to process.
Another note: when OnTimer was called you are in the main thread and in the message loop. Any waiting or blocking action would freeze the screen as no (more) messages were processed.
So, when using a message queue timer you could update the progress bar (and kill or renew the timer as appropriate) and return immediately but not have some lengthy code like performing a slow database query or even connect to another network device.
(2) Using a system timer in a thread
With that your main thread couldn't hang as the timer was in a new thread. You would invoke a new thread (worker thread) which sets a system timer. Then the thread would wait until the timeout. After awakening you would retrieve the progress state (somehow) and post a (private) message to the UI thread. The UI thread has a handler for that private message and would set the progress-bar according to the progress information given with the message.
Is it me, or are you looking for ProgressBar.Style = ProgressBarStyle.Marquee, without any kind of timer, or any other magic...
Windows Forms ProgressBar: Easiest way to start/stop marquee?
http://stackoverflow.com/q
Start:
progressBar1.Style = ProgressBarStyle.Marquee;
progressBar1.MarqueeAnimat
Stop:
progressBar1.Style = ProgressBarStyle.Block;
progressBar1.MarqueeAnimat
hi thats, perfect for my first screen!
however i have a second screen which includes a lengthy sql db creation, population, etc.
i have broken the sql statements down into chunks and store it in a static class.
now to monitor the progress of this i am going to use a public int in the static class containing the sql, so after each sql insert, etc i update the int as to the progress (i.e. add 1 to it after each query).
I then want the timer to poll this integer so it knows how far through the process we are and fill the progress bar accordingly.
so i will need a timer running in a seperate thread. Am i right in thinking that the Background worker is running in a seperate thread?
or should i go back to using the system.threading.thread?
many thanks for everyones help on this,
Matt.
sorry by this i mean i execute each create, insert, seperately.
so for example id execute
CREATE table .....
then update the progress by 1
CREATE second table
update progress
etc....
that way i know how far the program has got through the db creation. so if i poll the global int at any point the returned value should tell me the its progress? do this sound viable?
many thanks for your reply.
Matt.
If you have a BackgroundWorker, and you know how much work needs to be done, then you can use the ReportProgress method, which raises the ProcessChanged event.
BackgroundWorker.ReportPro
http://msdn.microsoft.com/
thanks sorry for the delay in the reply learnedone.
With this solution i am essentially passing the progress to the background worker.
however, i think in my case it is the other way around.
All of my sql, etc, is in contained in a static class.
so to explain more, from my windows form i call the sql insertion method which will run setting a global max_progress int (which gives the overall progress to be reched) and a progress int, this is the current progress.
i've been trying to get the background work to poll this every second and this is where the problem is as it (even though i backgroundworker is working on a seperate thread, it doesn't seem to be polling until the sql has completed.)
Now i suppose i could alter the method to pass a reference to the background worker being used and update it tht way? would you recommend doing this, so i could then use report progress that way? the only problem here is that i could just then pass a ref to the progress bar and manually update that then, which kind of defeats the point of running on a seperate thread?
many thanks for all youre help in advance,
Matt.
>>>> even though i backgroundworker is working on a seperate thread, it doesn't seem to be polling until the sql has completed
Indeed. If you have a lengthy SQL statement to query your background thread would not be able to report progress until all results were received.
There are two ways out:
(1) Show the progress in a timer function of the main thread but show no procentual progress but only the elapsed time until the background thread was done.
(2) Change the select statement so that it receives the results in chunks. E. g. if your data has a contiguous number as an attribute, determine MIN and MAX of that attribute at the beginning and now have - say - 10 queries where the first one would have 'and NUMBER_ATTRIBUTE >= :x and NUMBER_ATTRIBUTE < :y' as an additional condition where x was the minimum number and y was minimum + (maximum-minimum/10);
hi thanks for both your comments, as requested heres how i do it
in the button click event is start the background worker as follows;
backgroundWorker1.RunWorke
i then run my static class db creation method.
SqlClass.CreateDatabase(db
public static void createDatabase(String db)
{
//set max for reading completion status
max_progress = 28;
//create a connection
SqlConnection conn = createConn();
SqlDataReader reader = null;
//with create do a select to check exists
String sql = @" CREATE DATABASE " + db + ";" +
@" SELECT * FROM sys.databases WHERE name = '" + db + "'";
reader = executeSql(sql, ref conn);
if(reader == null)
{
CustomMessage customMessage = new CustomMessage();
throw new CustomException.CustomExce
}
reader.Close();
reader = null;
progress = 1;
sql = @" Use " + db + ";";
reader = executeSql(sql, ref conn);
reader.Close();
reader = null;
progress = 2;
sql = @" CREATE TABLE ....
IF EXISTS (...
) SELECT 1 AS res";
reader = executeSql(sql, ref conn);
if(reader == null)
{
CustomMessage customMessage = new CustomMessage();
throw new CustomException.CustomExce
}
reader.Close();
reader = null;
progress = 3;
sql = @" CREATE TABLE [room]...
IF EXISTS (...
) SELECT 1 AS res";
reader = executeSql(sql, ref conn);
if(reader == null)
{
CustomMessage customMessage = new CustomMessage();
throw new CustomException.CustomExce
}
reader.Close();
reader = null;
etc... up to 28
and the background worker method is as follows;
private void backgroundWorker1_DoWork(o
{
//invoke this and keep looking up the progress and update to the progress bar
System.Threading.Thread.Sl
Progress_Bar.Value = (SqlLogin.Progress * (100/SqlLogin.MaxProgress)
}
if you need anything else let me know.
many thanks for your time on this.
Matt.
Business Accounts
Answer for Membership
by: jungesPosted on 2009-09-12 at 11:41:25ID: 25317438
on you separate thread you have to call the Progress_Bar.Invoke method for all operations that interacts with the UI thread