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?
You need to perform the long running operation on a background thread.
There are several ways of doing this.
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());
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.
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.
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.
Obviously, you need to use background threads. I suggest you read this free e-book.
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.