Skip to content

Instantly share code, notes, and snippets.

@matthewjberger
Last active September 2, 2016 19:46
Show Gist options
  • Save matthewjberger/f0caa019816851c11e05baef73b303fc to your computer and use it in GitHub Desktop.
Save matthewjberger/f0caa019816851c11e05baef73b303fc to your computer and use it in GitHub Desktop.
Executes code from a wpf app on an STA thread synchronously and sets the cursor to busy.
void ExecuteOnSTAThread(ThreadStart t)
{
Mouse.OverrideCursor = Cursors.Wait; // Make the mouse cursor a busy spinner
Thread thread = new Thread(t);
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
thread.Join();
Mouse.OverrideCursor = null;
}
ExecuteOnSTAThread(new ThreadStart(() =>
{
try
{
// TODO: Your code goes here.
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
}));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment