CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jan 2014
    Posts
    4

    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 } } } }

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    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.

  3. #3
    Join Date
    Nov 2017
    Posts
    18

    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

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: How to access same variable in Main() and in thread proc in another class?

    Quote Originally Posted by CodeCake View Post
    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.

  5. #5
    Join Date
    Nov 2017
    Posts
    18

    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
  •  





Click Here to Expand Forum to Full Width

Featured