G
Guest
I am trying to unit test a control that uses a background worker component
and have discovered that the RunWorkerCompleted event does not fire in this
scenario. The documentation doesn't say anything about this behavior, and I
was wondering if anyone here could tell me why the RunWorkerCompleted event
does no fire in the following sample code:
using System;
using System.Threading;
using System.Windows.Forms;
using System.ComponentModel;
namespace BackgroundWorkerPrototype
{
class Program
{
static void Main( string[] args )
{
BackgroundWorkerUserControl control = new
BackgroundWorkerUserControl();
control.StartBackgroundWorker();
Thread.Sleep( 3000 );
}
}
class BackgroundWorkerUserControl : UserControl
{
private BackgroundWorker worker;
public BackgroundWorkerUserControl()
{
worker = new BackgroundWorker();
SuspendLayout();
worker.DoWork += new DoWorkEventHandler( worker_DoWork );
worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(
worker_RunWorkerCompleted );
}
public void StartBackgroundWorker()
{
worker.RunWorkerAsync();
}
private void worker_DoWork( object sender, DoWorkEventArgs e )
{
Console.WriteLine( "DoWork started." );
Thread.Sleep( 1000 );
Console.WriteLine( "DoWork completed." );
}
private void worker_RunWorkerCompleted( object sender,
RunWorkerCompletedEventArgs e )
{
Console.WriteLine( "RunWorkerCompleted called." );
}
}
}
Thanks,
John
and have discovered that the RunWorkerCompleted event does not fire in this
scenario. The documentation doesn't say anything about this behavior, and I
was wondering if anyone here could tell me why the RunWorkerCompleted event
does no fire in the following sample code:
using System;
using System.Threading;
using System.Windows.Forms;
using System.ComponentModel;
namespace BackgroundWorkerPrototype
{
class Program
{
static void Main( string[] args )
{
BackgroundWorkerUserControl control = new
BackgroundWorkerUserControl();
control.StartBackgroundWorker();
Thread.Sleep( 3000 );
}
}
class BackgroundWorkerUserControl : UserControl
{
private BackgroundWorker worker;
public BackgroundWorkerUserControl()
{
worker = new BackgroundWorker();
SuspendLayout();
worker.DoWork += new DoWorkEventHandler( worker_DoWork );
worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(
worker_RunWorkerCompleted );
}
public void StartBackgroundWorker()
{
worker.RunWorkerAsync();
}
private void worker_DoWork( object sender, DoWorkEventArgs e )
{
Console.WriteLine( "DoWork started." );
Thread.Sleep( 1000 );
Console.WriteLine( "DoWork completed." );
}
private void worker_RunWorkerCompleted( object sender,
RunWorkerCompletedEventArgs e )
{
Console.WriteLine( "RunWorkerCompleted called." );
}
}
}
Thanks,
John