title: Delegate invoke
body: <p>I want to change a Form control property from a process thread event fire in a class, I have the following code but I received this exception:</p>
  
  <blockquote>
    <p>the calling thread cannot access this object because a different
    thread owns it.</p>
  </blockquote>
  
  <p>Code:</p>
  
  <pre><code>public partial class main : Window
  {        
     public main()
     {
        InitializeComponent();
     }
  
     public void change()
     {
        label1.Content = "hello";
     }
  
     private void button1_Click(object sender, RoutedEventArgs e)
     {
        nmap nmap = new nmap(this);
        nmap.test("hello");
     }
  }
  
  class nmap
  {
      private main _frm;
      private Process myprocess;
  
      public nmap(main frm)
      {
         _frm = frm;
      }
  
      public void test(object obj)
      {
          string s1 = Convert.ToString(obj);
          ProcessStartInfo startInfo = new ProcessStartInfo();
          myprocess = new Process();
          myprocess.StartInfo.FileName = "C:\\nmap\\nmap.exe";
          myprocess.EnableRaisingEvents = true;
          myprocess.Exited += new EventHandler(myProcess_Exited);
  
          myprocess.Start();
      }
  
      private void myProcess_Exited(object sender, System.EventArgs e)
      {
         try
         {
            _frm.change();
         }
         catch{}
      }
  }
  </code></pre>
  
  <p>Please help me on this, I think delegate invoke must be work </p>
  
  <p>My project is a WPF C# project.</p>
  
  <p>answer is:</p>
  
  <pre><code>class nmap
      {
          private main _frm;
          private Process myprocess;
  
  
          public nmap()
          {
  
          }
          public nmap(main frm)
          {
              _frm = frm;
          }
          public void test(object obj)
          {
              string s1 = Convert.ToString(obj);
              ProcessStartInfo startInfo = new ProcessStartInfo();
              myprocess = new Process();
              myprocess.StartInfo.FileName = "C:\\nmap\\nmap.exe";
              //myprocess.StartInfo.CreateNoWindow = true;
              myprocess.EnableRaisingEvents = true;
              //myprocess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
              myprocess.Exited += new EventHandler(myProcess_Exited);
              myprocess.Start();
  
          }
  
          private void myProcess_Exited(object sender, System.EventArgs e)
          {
              try
              {
                  String s;
                  s = "hello";
                  _frm.Dispatcher.Invoke(_frm.USD, new Object[] { s });
              }
              catch{}
          }
  
      }
  
   public partial class main : Window
      {
          public delegate void UpdateStatusDelegate(string value);
          public UpdateStatusDelegate USD;
  
          public main()
          {
              InitializeComponent();
          }
  
          private void Window_Loaded(object sender, RoutedEventArgs e)
          {
              USD = new UpdateStatusDelegate(this.AddString);
  
          }
          private void AddString(String s)
          {
              label1.Content = s;
  
          }
          public void change()
          {
              label1.Content = "hello";
          }
  
  
          private void button1_Click(object sender, RoutedEventArgs e)
          {
              nmap nmap = new nmap(this);
              nmap.test("hello");
  
          }
  }
  </code></pre>