-
October 31st, 2017, 10:58 AM
#1
How to access same variable in Main() and in thread proc in another class?
This example has a member variable in the class "Program" named "ProgramDat" that I want to access from a thread in another class.
I want the thread to be able to change that memory location and have Program::Main() see the update as well as Program::Main() change the variable and have the thread see it.
ref doesn't seem to do it. Is there another construct I could use?
(And I recognize the potential race conditions here. I left out any synchronization techniques for simplicity of this post.)
Thanks.
Code:
using System;
using System.Threading;
public class Program
{
static ManualResetEvent ExitEvent;
static MyThread t;
static int ProgramDat;
public static void Main()
{
ExitEvent = new ManualResetEvent(false);
ProgramDat = 12; // initialize to some value
t = new MyThread(ExitEvent, ref ProgramDat);
Thread.Sleep(1500); // let MyThread run a bit
// Main() doesn't see the changes MyThread::RunLoop() made
Console.WriteLine("Main just before setting to 500, MyFormDat={0}", ProgramDat);
ProgramDat = 500; // and this change is not seen in MyThread::RunLoop();
Console.WriteLine("Main just set MyFormDat={0}", ProgramDat);
Thread.Sleep(2000);
Console.WriteLine("Just prior to telling thread to exit, MyFormDat={0}", ProgramDat);
ExitEvent.Set();
Thread.Sleep(500); // wait to let MyThread::RunLoop() finish
Console.WriteLine("main leaving. MyFormDat={0}", ProgramDat);
}
}
public class MyThread
{
ManualResetEvent e;
Thread t;
int MyThreadDat;
public MyThread(ManualResetEvent ev, ref int FromProgramDat)
{
e = ev;
MyThreadDat = FromProgramDat;
t = new Thread(RunLoop);
t.Start();
}
public void RunLoop()
{
while (true)
{
if (e.WaitOne(300) == true)
{
Console.WriteLine("RunLoop leaving!");
return;
}
else
{
Console.WriteLine("tic. iFormDat={0}", MyThreadDat);
MyThreadDat++; // change it each loop but I can't get Main() to see this change
}
}
}
}
-
November 1st, 2017, 12:18 AM
#2
Re: How to access same variable in Main() and in thread proc in another class?
Since you are using a static variable, you could make it public. That way you could access it directly without having to pass it to the thread. In your thread, change the MyThreadDat to:
Code:
MyThreadDat = Program.ProgramDat;
Btw, this would be okay for your simple example (ignoring race conditions). For real code, I would do something more robust.
-
November 9th, 2017, 08:43 PM
#3
Re: How to access same variable in Main() and in thread proc in another class?
What i would recommend is to use private variables and public getters and setters
-
November 10th, 2017, 02:18 AM
#4
Re: How to access same variable in Main() and in thread proc in another class?
Originally Posted by CodeCake
What i would recommend is to use private variables and public getters and setters
For C#, you might want to read up on properties and auto properties.
-
November 13th, 2017, 03:03 PM
#5
Re: How to access same variable in Main() and in thread proc in another class?
Idk sorry i talk based on my java knowledge
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|