This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

Run code on UI thread without control object present

By: a guest on Feb 26th, 2012  |  syntax: None  |  size: 2.80 KB  |  views: 20  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. System.Threading.SynchronizationContext.Current.Post(theMethod, state);
  2.        
  3. delegate UiSafeCall(delegate d, params object p);
  4. void SomeUiSafeCall(delegate d, params object p)
  5. {
  6.   if (InvokeRequired)
  7.     BeginInvoke(d,p);        
  8.   else
  9.   {
  10.     //do stuff to UI
  11.   }
  12. }
  13.        
  14. delegate UiSafeCall(delegate d, params object p);
  15. void SomeUiSafeCall(delegate d, params object p)
  16. {
  17.   if (this.InvokeRequired)
  18.     this.BeginInvoke(d,p);        
  19.   else
  20.   {
  21.     //do stuff to UI
  22.   }
  23. }
  24.        
  25. public class MyBackgroundThread : BackgroundWorker
  26. {
  27.     public event EventHandler<ClassToPassToUI> IWantTheUIToDoSomething;
  28.  
  29.     public MyStatus TheUIWantsToKnowThis { get { whatever... } }
  30.  
  31.     public void TheUIWantsMeToDoSomething()
  32.     {
  33.         // Do something...
  34.     }
  35.  
  36.     protected override void OnDoWork(DoWorkEventArgs e)
  37.     {
  38.         // This is called when the thread is started
  39.         while (!CancellationPending)
  40.         {
  41.             // The UI will set IWantTheUIToDoSomething when it is ready to do things.
  42.             if ((IWantTheUIToDoSomething != null) && IHaveUIData())
  43.                 IWantTheUIToDoSomething( this, new ClassToPassToUI(uiData) );
  44.         }
  45.     }
  46. }
  47.  
  48.  
  49. public partial class MyUIClass : Form
  50. {
  51.     MyBackgroundThread backgroundThread;
  52.  
  53.     delegate void ChangeUICallback(object sender, ClassToPassToUI uiData);
  54.  
  55.     ...
  56.  
  57.     public MyUIClass
  58.     {
  59.         backgroundThread = new MyBackgroundThread();
  60.  
  61.         // Do this when you're ready for requests from background threads:
  62.         backgroundThread.IWantTheUIToDoSomething += new EventHandler<ClassToPassToUI>(SomeoneWantsToChangeTheUI);
  63.  
  64.         // This will run MyBackgroundThread.OnDoWork in a background thread:
  65.         backgroundThread.RunWorkerAsync();
  66.     }
  67.  
  68.  
  69.     private void UserClickedAButtonOrSomething(object sender, EventArgs e)
  70.     {
  71.         // Really this should be done in the background thread,
  72.         // it is here as an example of calling a background task from the UI.
  73.         if (backgroundThread.TheUIWantsToKnowThis == MyStatus.ThreadIsInAStateToHandleUserRequests)
  74.             backgroundThread.TheUIWantsMeToDoSomething();
  75.  
  76.         // The UI can change the UI as well, this will not need marshalling.
  77.         SomeoneWantsToChangeTheUI( this, new ClassToPassToUI(localData) );
  78.     }
  79.  
  80.     void SomeoneWantsToChangeTheUI(object sender, ClassToPassToUI uiData)
  81.     {
  82.         if (InvokeRequired)
  83.         {
  84.             // A background thread wants to change the UI.
  85.             if (iAmInAStateWhereTheUICanBeChanged)
  86.             {
  87.                 var callback = new ChangeUICallback(SomeoneWantsToChangeTheUI);
  88.                 Invoke(callback, new object[] { sender, uiData });
  89.             }
  90.         }
  91.         else
  92.         {
  93.             // This is on the UI thread, either because it was called from the UI or was marshalled.
  94.             ChangeTheUI(uiData)
  95.         }
  96.     }
  97. }
clone this paste RAW Paste Data