Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to execute method calls of a custom class in a separate thread?
    primarykey
    data
    text
    <p>I have an UI, a custom class, and a thread. I want to run the custom class completely in a separate thread. Is there a clean way of doing this?</p> <p>For example. On the MainForm below, when UI calls _threadOneClass.Sleep, I need the UI to go to the spawned ThreadOne and invoke the Sleep method in ThreadOne, not in the main thread.</p> <p>Basically, all method calls in MyClass need to be executed in ThreadOne, not in main thread. It is like, the MyClass runs on its own "process", while still visible to be called from MainForm.</p> <p>The MainForm has 3 buttons, and 1 textbox for logging.</p> <p>I was thinking of deriving the Thread class, but it is sealed. So deriving is definitely a wrong way per Microsoft.</p> <p>Help dear experts?</p> <p>Here is the output (MainThread ID=10, ThreadOne ID=11)</p> <pre> MyClass instantiated Starting ThreadOne 11-Run.start Sleeping ThreadOne 10-Run.sleep for 3000 'Need this to run on ThreadID 11 10-Run.woke up 'Need this to run on ThreadID 11 Stopping ThreadOne 11-Run.done </pre> <p>Here is how the code look like.</p> <pre><code>public partial class MainForm : Form { public MainForm() { InitializeComponent(); } private Thread _threadOneThread; private MyClass _threadOneClass; private void btnThreadOneCreate_Click(object sender, EventArgs e) { _threadOneClass = new MyClass(this); _threadOneThread = new Thread(new ThreadStart(_threadOneClass.Run)); _threadOneThread.Start(); } private void btnThreadOneStop_Click(object sender, EventArgs e) { _threadOneClass.IsRunning = false; } private void btnThreadOneSleep_Click(object sender, EventArgs e) { _threadOneClass.Sleep(3000); } public void Log(string txt) { MainForm.SetText(txtLog, txt); } internal static void SetText(Control ctl, string val) { if (ctl.InvokeRequired) ctl.Invoke((MethodInvoker)delegate() { ctl.Text += Environment.NewLine + val; }); else ctl.Text += Environment.NewLine + val; } } class MyClass { public MyClass(MainForm frm) { _mainForm = frm; } private MainForm _mainForm; public bool IsRunning = true; public void Run() { _mainForm.Log(Thread.CurrentThread.ManagedThreadId.ToString() + "-Run.start"); while (IsRunning) { } _mainForm.Log(Thread.CurrentThread.ManagedThreadId.ToString() + "-Run.done"); } public void Sleep(int milliseconds) { _mainForm.Log(Thread.CurrentThread.ManagedThreadId.ToString() + "-Run.sleep for " + milliseconds.ToString()); Thread.Sleep(milliseconds); _mainForm.Log(Thread.CurrentThread.ManagedThreadId.ToString() + "-Run.woke up"); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload