title: How to pass or make public variables or fields accessible to Tthread?
body: <p>I am writing and building my software for Mono using Delphi Prism. So, I decided that my serial communication will be handled by a thread. Since global variables strictly are not allowed unless you enable the global variable option for the project, I decided to follow the Delphi Prism convention. So, then how do you pass or make the public variables or fields accessible to a thread?</p>
  
  <p>Here is my test mainform code:</p>
  
  <pre><code>MainForm = partial class(System.Windows.Forms.Form)
  
    private
      method SignalBtn_Click(sender: System.Object; e: System.EventArgs);
      method CommBtn_Click(sender: System.Object; e: System.EventArgs);
      method button1_Click(sender: System.Object; e: System.EventArgs);
      method button2_Click(sender: System.Object; e: System.EventArgs);
      method button4_Click(sender: System.Object; e: System.EventArgs);
      method button5_Click(sender: System.Object; e: System.EventArgs);
      method MainForm_Load(sender: System.Object; e: System.EventArgs); 
      method ShutdownBtn_Click(sender: System.Object; e: System.EventArgs);
      method MySerialData(sender: System.Object; e:SerialDataReceivedEventArgs);
      method LoginBtn_Click(sender: System.Object; e: System.EventArgs);
    protected
      method Dispose(disposing: Boolean); override;
    public
      RX:Array[0..5] of byte;
      TX:Array[0..6] of byte;
      serialPort1:System.IO.Ports.SerialPort;
      thr:Thread;
      stoploop:Boolean;
      mcommand:Byte;
      thechannel:Integer;
      constructor;
      method FillTable;
    end;
  </code></pre>
  
  <p>Here is the Thread for serial communication:</p>
  
  <pre><code>  ThreadComm = class(MainForm)
    public
      class procedure mythread; static;
    end;
  </code></pre>
  
  <p>Here is how ThreadComm runs:</p>
  
  <pre><code>class procedure ThreadComm.mythread;
  begin
      while true do
      begin
          TX[0]:=$FF;
          TX[1]:=$01;
          TX[2]:=$01;
          TX[3]:=$04;
          TX[4]:=$A2;
          TX[5]:=(TX[2] xor TX[3] xor TX[4]);
  
          SerialPort1.Write(TX,0,6);
          while SerialPort1.BytesToWrite&gt;0 do;
          Thread.Sleep(100);
  
          if (stoploop) then
              break;
      end;
  end;
  </code></pre>
  
  <p>Every time I compile the code, it raises 30 or so similar error messages stating the following:</p>
  
  <p><strong>Cannot call instance member "SerialPort1" without an instance reference</strong></p>
  
  <p>I know what the error means, but the only way to solve it is by creating an instance of the mainform. If you do that, then it won't be the same instance as the main program's instance. If this is the case, then you will have to create new instance of mainform all the time when you need to access its fields or public variables. Right?</p>
  
  <pre><code>class method Program.Main(args: array of string);
  begin
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Application.ThreadException += OnThreadException;
    **using lMainForm := new MainForm do
      Application.Run(lMainForm);**
  end;
  </code></pre>
  
  <p>I want to use all them variables are in the thread and they happen to be within the mainform public area.</p>
  
  <p>Thanks</p>