Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6132149
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:03:56+00:00 2026-05-23T17:03:56+00:00

I am writing and building my software for Mono using Delphi Prism. So, I

  • 0

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?

Here is my test mainform 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;

Here is the Thread for serial communication:

  ThreadComm = class(MainForm)
  public
    class procedure mythread; static;
  end;

Here is how ThreadComm runs:

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>0 do;
        Thread.Sleep(100);

        if (stoploop) then
            break;
    end;
end;

Every time I compile the code, it raises 30 or so similar error messages stating the following:

Cannot call instance member “SerialPort1” without an instance reference

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?

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;

I want to use all them variables are in the thread and they happen to be within the mainform public area.

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T17:03:57+00:00Added an answer on May 23, 2026 at 5:03 pm

    Your thread procedure (class procedure mythread; static;) seems to be “static” or “class”, while the the fields you want to access are regular fields of a MainForm instance. That can’t work, because there’s one and only one procedure mythread, but there may be multiple instances of MainForm. While in practice there will probably only be one instance of MainForm, the compiler doesn’t know that.

    You should start by removing both class prefix and static sufix from that procedure, to make it a regular instance procedure, that can read instance fields. Even that’s not going to be enough, because you derived from your MainForm, and I assume you create a new instance of ThreadCom and start the thread: Once you make the method a regular instance method the code will compile, but the result isn’t going to be the one you expect, because you’ll be accessing members of a different instance of MainForm.

    Solutions:

    • Make the mythread procedure a instance method of your MainForm. It’ll gain access to all required fields and access them from the proper instance!
    • Follow Rob’s suggestion: create a new class, not derived from MainForm, pass it an instance of MainForm in the constructor, you that from the thread procedure. You’ll (again) need to avoid static/class fields and procedures, because those can’t be linked to one instance of MainForm.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If the Mono project is successful it will pave the way for commercial software
I'm currently writing a building script that needs the installdir registry key for visual
I am writing a script to automate my iOS building. It will be run
I'm writing a geocoding component for an app I'm building, and I decided to
I'm building little class called Output ... It's meant to be something that will
I am writing a loop that is building a table. With a mysql_num_row ,
I'm building a program that relies on a old legacy system. I'm especially writing
The naive way of writing building a menu in a Java Swing app is
I'm working on building the URLs for my REST API before I begin writing
Writing a JSP page, what exactly does the <c:out> do? I've noticed that the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.