Re: VS 2003 progressbar thread

Tech-Archive recommends: Fix windows errors by optimizing your registry



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]
- mvp@xxxxxxxxxxxxxxxxxxxxxxxxxxx

"Chris" <Chris@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:88D40742-4781-421F-AE99-27EAB02D3C2B@xxxxxxxxxxxxxxxx
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


.



Relevant Pages

  • Re: close excel from VB6
    ... Indeed all well known in Excel land. ... In VBA to do otherwise would cause the app to blow (if the ... MSO app from a VB6.exe or are a MSO addin interfacing with VB6.DLLs. ... learning programming was a good idea because my years of business ...
    (microsoft.public.vb.general.discussion)
  • RE: Ribbon primarily designed to drive sales rather than improve usabi
    ... using a new app or OS you may ... regularly with Excel 2003. ... looking for, can you believe it, edit, replace!!! ... I see there is a tool to customize the ribbon which cost $30 at think ...
    (microsoft.public.office.misc)
  • Re: Prevent multiple instances of Excel from starting
    ... I didn't have any confusion as regards multiple Excel instances vs multiple ... Does your app call Excel to reference a particular file (which may or may ... I don't have some malicious intent on making life difficult by randomly ...
    (microsoft.public.excel.programming)
  • RE: Fastest way to read data to populate combo controls?
    ... the type array the read all in one get. ... Or Using VBA in Excel? ... and the VB app opens that document. ... The Word doc is actually built using a macro in an Excel file. ...
    (microsoft.public.vb.general.discussion)
  • RE: Export options
    ... I'm trying to implement your code in my VB app, and I think it will work - ... - but my VB6 is more than a little rusty, ... to do it from this script anyway. ... manipulates each Excel file using that object's methods etc. ...
    (microsoft.public.excel.programming)