Skip to content

Instantly share code, notes, and snippets.

@thedersen
Created August 4, 2010 15:10
Show Gist options
  • Save thedersen/508277 to your computer and use it in GitHub Desktop.
Save thedersen/508277 to your computer and use it in GitHub Desktop.
public partial class Form : System.Windows.Forms.Form
{
public Form()
{
InitializeComponent();
}
private void buttonStart_Click(object sender, EventArgs e)
{
StartAsyncJob();
}
private void StartAsyncJob()
{
var worker = new Worker(SynchronizationContext.Current);
backgroundWorker.DoWork += (sender, e) => worker.DoWork();
backgroundWorker.RunWorkerAsync();
}
}
public class Worker
{
private readonly SynchronizationContext _synchronizationContext;
public Worker(SynchronizationContext synchronizationContext)
{
_synchronizationContext = synchronizationContext;
}
public void DoWork()
{
_synchronizationContext.Send(callback => MessageBox.Show("Hello from background worker!"), null);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment