title: C# Access variables of different threads from a thread
body: <p>So there's the situation - When the console application starts, it creates 3 threads that stay there forever and do whatever they are supposed to do. Any of those threads will then be able to show up a windows form as a separate thread so it doesn't wait for it to return anything using this code:</p>
  
  <pre><code>public void RunThread()
  {
      Thread thread = new Thread(new ThreadStart(RunForm));
      thread.Name = "StatusForm";
      thread.Start();
  }
  
  public void RunForm()
  {
      Application.Run(new StatusForm());
  }
  </code></pre>
  
  <p>This windows form will then attempt to get some variables from all 3 threads, I do that using:</p>
  
  <pre><code>int var1 = Manager1.InventoryEntriesOne
  int var2 = Manager2.InventoryEntriesTwo
  int var3 = Manager3.InventoryEntriesThree
  </code></pre>
  
  <p>The InventoryEntries in the first thread is declared like this (Same goes for other 2 threads):</p>
  
  <pre><code>public static volatile InventoryEntries
  </code></pre>
  
  <p>Now for some reason what happens is the windows form only shows the variables of the thread that has brought up the window using the <code>RunThread()</code> function. For example if Thread 1 starts the window, only the variables of that thread will be shown up, for Thread 2 and 3 it will be all 0. It needs to be able to read variables from all 3 threads, doesn't matter which thread launched the window.</p>
  
  <p>Thank you.</p>
  
  <p><strong>-----------------EDIT-------------------</strong></p>
  
  <p>Let me just show you a brief example of what is happening inside of the code. This is example of Manager1, assume that 2 other managers are doing basically the same thing...</p>
  
  <pre><code>using System.Windows.Forms;
  using System.Threading;
  
  namespace Manager
  {
      public class Manager1 : Manager
      {
          public static volatile int InventoryEntriesOne = 123;
  
  
          public override bool OnSomeEvent()
          {
              InventoryEntriesOne = 555;
          }
  
          public override bool OnSomeOtherEvent()
          {
              RunThread();
          }
  
          public void RunThread()
          {
              Thread thread = new Thread(new ThreadStart(RunForm));
              thread.Name = "StatusForm";
              thread.Start();
          }
          public void RunForm()
          {
              Application.Run(new StatusForm());
          }
      }
  }
  </code></pre>
  
  <p>So as you can see, the Manager is opening a windows form, now what happens in that form is this:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.ComponentModel;
  using System.Data;
  using System.Drawing;
  using System.Linq;
  using System.Text;
  using System.Windows.Forms;
  
  namespace Manager
  {
      public partial class StatusForm : Form
      {
          Timer refreshTimer = new Timer();
  
          public StatusForm()
          {
              InitializeComponent();
  
              PlotData();
  
              refreshTimer.Interval = 5000;  //5 seconds in milliseconds  
              refreshTimer.Tick += new EventHandler(refreshTimer_Tick);
              refreshTimer.Start();
          }
  
          void refreshTimer_Tick(object sender, EventArgs e)
          {
              PlotData();
          }
  
          public void PlotData()
          {
              label1.Text = Manager1.InventoryEntriesOne.ToString();
  
              label2.Text = Manager2.InventoryEntriesTwo.ToString();
  
              label3.Text = Manager3.InventoryEntriesThree.ToString();
          }
      }
  }
  </code></pre>
  
  <p>I hope you guys will understand the problem now.</p>