Lengthy operation in Win Form application

  • Thread starter Thread starter Hardy Wang
  • Start date Start date
H

Hardy Wang

Hi,
I have a win form application, when a button is clicked, a lengthy
operation will be triggered. During the time program is still running, this
application seems not to be able to response to other actions. When I switch
to other window, and switch back to my application, all I can see is a white
window, it won't refresh itself until operation finishes.
If I have some other buttons in the form, it is not possible to show up
during operation. What is the way I can let my operation to run, but
meanwhile application can accept other GUI events?

Thanks
Hardy
 
Hardy said:
Hi,
I have a win form application, when a button is clicked, a lengthy
operation will be triggered. During the time program is still running, this
application seems not to be able to response to other actions. When I switch
to other window, and switch back to my application, all I can see is a white
window, it won't refresh itself until operation finishes.
If I have some other buttons in the form, it is not possible to show up
during operation. What is the way I can let my operation to run, but
meanwhile application can accept other GUI events?

Thanks
Hardy

If you're application is written completely in .NET, consider executing
your operation in a thread. It's fairly simple to do, but you'll have
to put in some extra code to disable the button (unless you want
multiple threads of the same operation running).

Here's the MSDN docs on System.Threading:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemthreading.asp

All you need is a function to create a thread, so I'd wrap whatever that
operation is within a function.

If your lengthy operation is not .NET, try executing it as an external
process, and not using P/Invoke to make a DLL call. You might be able
to wrap the P/Invoke call in a thread too, but I'm unsure of that.
 
Hi,
I have a win form application, when a button is clicked, a lengthy
operation will be triggered. During the time program is still running, this
application seems not to be able to response to other actions. When I switch
to other window, and switch back to my application, all I can see is a white
window, it won't refresh itself until operation finishes.
If I have some other buttons in the form, it is not possible to show up
during operation. What is the way I can let my operation to run, but
meanwhile application can accept other GUI events?

You'll need to start up another thread to run your code on. Basically,
windows forms start with a single thread which contains the message pump
(the OS is sending all of the windows messages to your app -- paint,
mouse clicks, key clicks, etc...) If your app is busy doing a long-
running process, it won't be able to respond to those events (like
paint) and your UI will suffer.

Look into threading and also be mindful of updating the UI from a
background thread (need to use Control.Invoke for this).
 
Hardy Wang said:
I have a win form application, when a button is clicked, a lengthy
operation will be triggered. During the time program is still running, this
application seems not to be able to response to other actions. When I switch
to other window, and switch back to my application, all I can see is a white
window, it won't refresh itself until operation finishes.
If I have some other buttons in the form, it is not possible to show up
during operation. What is the way I can let my operation to run, but
meanwhile application can accept other GUI events?

See
http://www.pobox.com/~skeet/csharp/multithreading.html
and in particular
http://www.pobox.com/~skeet/csharp/multithreading.html#windows.forms
 
Back
Top