A
AMI
I started with a fairly simple task: Displaying a progressbar on a WinForm
for a lengthy download over HTTP. I soon arrived at the question: What is
the best way to start a worker thread and why?
As I see it, the framework gives us the ThreadStart delegate to do this, and
also the BeginXXX/EndXXX model for asynchronous operations. The ThreadStart
delegate usage is quite simple to understand. I've come to understand the
other model, and now my refined question is: What benefits of going with
the other model?
I wrote some pseudocode below to illustrate the two different approaches to
the same problem, and I can't see why the heck one would choose the latter.
Thanks for any feedback!
------------------
ThreadStart approach
------------------
private void MainMethod()
{
Thread thread = new Thread(new ThreadStart(this.DownloadFile));
thread.Start();
thread.Join();
MessageBox.Show("I'm done.")
return;
}
private void DownloadFile()
{
// Do some work
// Update progressbar
// Do more work
// Update progressbar
}
--------------------------
Asynchronous method approach
--------------------------
private void MainMethod()
{
IAsyncResult result = BeginDownloadFile(new
AsyncCallback(EndDownloadFile));
result.WaitHandle.WaitOne();
MessageBox.Show("I'm done.")
return;
}
private IAsyncResult BeginDownloadFile(AsyncCallBack callback)
{
// Set up my own worker thread, point it to DownloadFile using
ThreadStart
// Somehow register the callback method to a Thread complete event (????)
// Start my thread
// Package up my homerolled MyAsyncResult object and return it
}
private void DownloadFile()
{
// Do some work
// Update progressbar
// Do more work
// Update progressbar
}
private void EndDownloadFile(IAsyncResult result)
{
...
}
public class MyAsyncResult : IAsyncResult
{
...
}
for a lengthy download over HTTP. I soon arrived at the question: What is
the best way to start a worker thread and why?
As I see it, the framework gives us the ThreadStart delegate to do this, and
also the BeginXXX/EndXXX model for asynchronous operations. The ThreadStart
delegate usage is quite simple to understand. I've come to understand the
other model, and now my refined question is: What benefits of going with
the other model?
I wrote some pseudocode below to illustrate the two different approaches to
the same problem, and I can't see why the heck one would choose the latter.
Thanks for any feedback!
------------------
ThreadStart approach
------------------
private void MainMethod()
{
Thread thread = new Thread(new ThreadStart(this.DownloadFile));
thread.Start();
thread.Join();
MessageBox.Show("I'm done.")
return;
}
private void DownloadFile()
{
// Do some work
// Update progressbar
// Do more work
// Update progressbar
}
--------------------------
Asynchronous method approach
--------------------------
private void MainMethod()
{
IAsyncResult result = BeginDownloadFile(new
AsyncCallback(EndDownloadFile));
result.WaitHandle.WaitOne();
MessageBox.Show("I'm done.")
return;
}
private IAsyncResult BeginDownloadFile(AsyncCallBack callback)
{
// Set up my own worker thread, point it to DownloadFile using
ThreadStart
// Somehow register the callback method to a Thread complete event (????)
// Start my thread
// Package up my homerolled MyAsyncResult object and return it
}
private void DownloadFile()
{
// Do some work
// Update progressbar
// Do more work
// Update progressbar
}
private void EndDownloadFile(IAsyncResult result)
{
...
}
public class MyAsyncResult : IAsyncResult
{
...
}