Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WinForm Application UI Hangs during Long-Running Operation

I have a windows forms application on which I need to use a for loop having a large number of Remote Calls around 2000 - 3000 calls,

and while executing the for loop, I loose my control on form and form controls, as it becomes a large process and some time it shows "Not Responding" but if I wait for a long it comes back again, I think I need to use some threading model for that, is there any idea, how can I proceed to solve the issue?

like image 854
shahjapan Avatar asked Aug 01 '09 15:08

shahjapan


People also ask

How do I turn off WinForm?

When we need to exit or close opened form then we should use "this. Close( )" method to close the form on some button click event. When we are running a winform application & need to exit or close SUB APPLICATION or CURRENT THREAD then we should use "System. Windows.

Is WinForm deprecated?

Win Form has been used to develop many applications. Because of its high age (born in 2003), WinForm was officially declared dead by Microsoft in 2014. However, Win Form is still alive and well.

How do I run WinForm programs when my computer starts?

The Run key (And alternatively the RunOnce key for running your application once) will run all applications that are in it on startup/when a user logs on. This is the code I use in my applications, and it works great.

How do I release WinForm app?

Click the sign in option and select the checkbox (Sign the Click Once manifests). Go to Security >> select Check Box (Enable Click Once Security Settings). Go to Publish >> select Publishing folder location path and Save. Click "Publish Now" button.


2 Answers

You need to perform the long running operation on a background thread.

There are several ways of doing this.

  1. You can queue the method call for execution on a thread pool thread (See here):

    ThreadPool.QueueUserWorkItem(new WaitCallback(YourMethod)); 

    In .NET 4.0 you can use the TaskFactory:

    Task.Factory.StartNew(() => YourMethod()); 

    And in .NET 4.5 and later, you can (and should, rather than TaskFactory.StartNew()) use Task.Run():

    Task.Run(() => YourMethod()); 
  2. You could use a BackgroundWorker for more control over the method if you need things like progress updates or notification when it is finished. Drag the a BackgroundWorker control onto your form and attach your method to the dowork event. Then just start the worker when you want to run your method. You can of course create the BackgroundWorker manually from code, just remember that it needs disposing of when you are finished.

  3. Create a totally new thread for your work to happen on. This is the most complex and isn't necessary unless you need really fine grained control over the thread. See the MSDN page on the Thread class if you want to learn about this.

Remember that with anything threaded, you cannot update the GUI, or change any GUI controls from a background thread. If you want to do anything on the GUI you have to use Invoke (and InvokeRequired) to trigger the method back on the GUI thread. See here.

like image 125
Simon P Stevens Avatar answered Oct 02 '22 09:10

Simon P Stevens


    private voidForm_Load(object sender, EventArgs e)     {         MethodInvoker mk = delegate         {             //your job         };         mk.BeginInvoke(callbackfunction, null);     }      private void callbackfunction(IAsyncResult res)     {         // it will be called when your job finishes.     } 

use MethodInvoker is the easiest way.

like image 28
VOX Avatar answered Oct 02 '22 10:10

VOX