VS 2003 progressbar thread

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I want to create an app in VS 2003 (not in 2005 !!).
It works this way: user chooses an excel file in openfiledialog. then he
clicks OK and a new form appears. There is a progressbar on the form. It
shows progress of reading the file. In the form I put FormLoad event:

System.Threading.Thread thr=new System.Threading.Thread(Read);
thr.Start();

and Read() function:

void Read()
{
while(/*read next row in the file*/)
{
/*insert the row into a database*/
/*...........*/
progressbar1.Increment(1);
}
}


it seems to work ok. But no-one has taught me how to use threads,. Is it a
good way? I am a newbie... I dont use any Invoke/BeginInvoke methods. Should
I?



The problem is that sometimes the app works fine, sometimes not.
Sometimes all of the rpows from the file are read and inserted to the db but
so,etimes only a few of them and the app freezes. Why?


There is also red cross on the form which closes the form. What is the best
way to handle the event when user clicks it? I mean when user clicks it, a
messegebox should apper "Stop the reading process?" Yes/No.

I did it this way. Whe user clicks Yes i call thr.Abort(). It works ok - the
app stops working but after a while I am getting message: The thread wa being
stopped? How to avoid that?

thx in advance
 
Are you setting the ApartmentState of your threat to STA? You might
have to do that in order to work with excel through COM interop.

That aside, you should be passing a delegate to the Invoke method from
your thread routine which will update the progress bar. This delegate will
point to the method to be called on the UI thread.

As far as cancelling the operation, if the user selects to do so (after
your validation dialog), you can set a flag which your Read method will
check (do it once per iteration). If the flag is set, then clean up, and
just return from the method, effectively ending the thread. Of course, make
sure that you have synchronized access to the flag (I suggest using the lock
keyword for this).
 
Nicholas Paldino [.NET/C# MVP] said on 14.8.2007 21:10:
Are you setting the ApartmentState of your threat to STA? You might
have to do that in order to work with excel through COM interop.

That aside, you should be passing a delegate to the Invoke method from
your thread routine which will update the progress bar. This delegate will
point to the method to be called on the UI thread.

As far as cancelling the operation, if the user selects to do so (after
your validation dialog), you can set a flag which your Read method will
check (do it once per iteration). If the flag is set, then clean up, and
just return from the method, effectively ending the thread. Of course, make
sure that you have synchronized access to the flag (I suggest using the lock
keyword for this).

Synchronized access to a boolean flag. Is this really necessary.
I'm new to .NET but I would accept boolean assign to be atomic, so
I do not see need for sync.
Can you enlighten my little newbie me?
 
Updates to a bool are indeed atomic, but that alone doesn't stop it
getting placed in a register - i.e. here:

class Waiter {
bool keepWaiting = true;
void WaitForSomething() {
while(keepWaiting) {Thread.Sleep(100);} // for illustration
only!!!
}
}

Another thread can set waitFlag = false, but the above might not exit.
For this to work, either you need a memory barrier (which lock()
causes by necessity), or you need the field to be volatile:

volatile bool keepWaiting = true;

the above means that the field is never cached in a register.

Marc
 
Back
Top