views:

1160

answers:

4

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?

Thanks

A: 

Obviously, you need to use background threads. I suggest you read this free e-book.

Wintorez
+11  A: 

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));

Or in .Net 4.0 you can use the TaskFactory:

Task.Factory.StartNew(() => 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 canont 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.

Simon P Stevens
I think this will help me, I'll try BackGroundWorker !
Tumbleweed
A: 

No thread needed. Use Application.DoEvents(); //repaint or respond to msg For example: foreach (DirectoryInfo subDir in dirInfo.EnumerateDirectories()) { count = count + 1; listBoxControl4.Items.Add(count.ToString() + ":" + subDir.FullName); Application.DoEvents(); //allow repaint to see status ProduceListing(subDir, " "); } This will go thru all folders recursively and write the names to a list box. This may take a long time. With the DoEvents(), it will show progress on each loop. Basically, the call allows windows on this thread to update anything in the windows msg loop. Make sure to watch for unintentional recursion as any control on the form an be clicked again even though your first has not completed. This works well. (It's a copy of the old Delphi Application.ProcessMessages() call. You can search for that to see why it works and what to watch for.

JoeJoe
@JoeJoe - Calling `DoEvents` is a bad thing. It can cause all sorts of threading issues, such as re-entrancy. It should be avoided entirely.
Enigmativity
A: 
    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.

VOX