Threading

  • Thread starter Charles A. Lackman
  • Start date
C

Charles A. Lackman

Hello,

I made a Windows Project in 1.1 and converted it to 2.0 and get the
following Error:

"Cross-thread operation not valid: Control 'progressBar1' accessed from a
thread other than the thread it was created on."

I have a small form that is processing a lot of information and if the user
moves the window or opens a new one and closes it, the form is not redrawn.
You just get a while window.

I managed to work around this in 1.1 using a new thread to do all the work
and to refresh the form and a progress bar. Unfortunately, this does not
work in 2.0.

Any Suggestions will be greatly appreciated.

Thanks,

Chuck
 
J

Jon Skeet [C# MVP]

Charles A. Lackman said:
I made a Windows Project in 1.1 and converted it to 2.0 and get the
following Error:

"Cross-thread operation not valid: Control 'progressBar1' accessed from a
thread other than the thread it was created on."

I have a small form that is processing a lot of information and if the user
moves the window or opens a new one and closes it, the form is not redrawn.
You just get a while window.

I managed to work around this in 1.1 using a new thread to do all the work
and to refresh the form and a progress bar. Unfortunately, this does not
work in 2.0.

It doesn't *really* work in 1.1 either - not reliably. You should
always have been using Control.Invoke or Control.BeginInvoke to make
sure the UI work is done on the UI thread. It's just that 2.0 gives you
an error if you try to do it (in debug mode only).

See http://www.pobox.com/~skeet/csharp/threads/winforms.shtml for
information on how to do it properly.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top