Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
swdev:dotnet:actions [2011/01/26 16:11]
smayr [Using Actions to Communicate Between Background Worker and Main Threads]
swdev:dotnet:actions [2011/01/26 16:55] (current)
smayr [Example]
Line 13: Line 13:
  
 <code csharp> <code csharp>
-using System.Windows.Threading;+using System.Windows.Threading;  // in WindowsBase.dll
  
 public partial class MyWindow : Window public partial class MyWindow : Window
Line 40: Line 40:
   * [[http://www.albahari.com/threading/part2.aspx#_Rich_Client_Applications|Threading (Part 2): Rich Client Applications and Thread Affinity]]   * [[http://www.albahari.com/threading/part2.aspx#_Rich_Client_Applications|Threading (Part 2): Rich Client Applications and Thread Affinity]]
  
 +== Example ==
 +
 +<code csharp>
 +///----------------------------------------------------------------------------------------
 +/// <summary>
 +/// Close the popup menu.
 +/// </summary>
 +///----------------------------------------------------------------------------------------
 +public void Close()
 +{
 +    //----------------------------------------------------------------------------
 +    // Deal with cross thread ownership of UI control instance
 +    // by invoking an action delegate. In other words, 
 +    // popupmnu is owned by thread that instantiated it (when Open() was called),
 +    // so we cannot close popmnu arbitrarily.  We need the help of
 +    // the owner thread's Dispatcher to execute the Close() for us.
 +    //----------------------------------------------------------------------------
 +    //Action action = () => popmnuReports.IsOpen = false;
 +    Action action = this.CloseDelegate();
 +    this.Dispatcher.BeginInvoke(action);
 +}
 +
 +///----------------------------------------------------------------------------------------
 +/// <summary>
 +/// Close method deletegate, used by cross thread dispatcher.
 +/// </summary>
 +/// <returns></returns>
 +///----------------------------------------------------------------------------------------
 +private Action CloseDelegate()
 +{
 +    return delegate()
 +    {
 +        popmnuFitting.IsOpen = false;
 +        m_TimerForDisplay.Enabled = false;
 +    };
 +}
 +</code>
 == Resources == == Resources ==
   * [[http://dotnetperls.com/action|DotNetPerls: Action]]   * [[http://dotnetperls.com/action|DotNetPerls: Action]]