Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have main program

static class Program
{
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Worker w1 = new Worker(1);
        Worker w2 = new Worker(2);
        Thread w1Thread = new Thread(new ThreadStart(w1.StartWorking));
        Thread w2Thread = new Thread(new ThreadStart(w2.StartWorking));
        w1Thread.Start();
        w2Thread.Start();
        Application.Run(new MainWindow());
        if (w1Thread.IsAlive)
        {
            w1Thread.Abort();
        }
        if (w2Thread.IsAlive)
        {
            w2Thread.Abort();
        }
    }
}

and worker class:

class Worker
{
    public int m_workerId;
    public bool m_workerLifeBit;
    public bool m_workerWork;

    public Worker(int id)
    {
        m_workerId = id;
        m_workerLifeBit = false;
    }
    public void StartWorking()
    {
        while (!m_workerWork)
        {
            m_workerLifeBit = false;
            System.Threading.Thread.Sleep(5000);
            m_workerLifeBit = true;
            System.Threading.Thread.Sleep(5000);
        }
    }
}

I have checkBox on MainWindow form. How to monitor state of Worker variable m_workerLifeBit and display its changes in MainWindow checkBox?

I have found this q&a How to update the GUI from another thread in C#? hovewer the answer does not show complete example, and I failed with using thread safe delegate.

I want some event mechanism that I fire in Worker.StartWorking and catch in slot in MainWindow form.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
146 views
Welcome To Ask or Share your Answers For Others

1 Answer

As mentioned in the comments, if this is a WinForms application then I'd recommend using a BackgroundWorker.

Kicking off the bg worker and subscribing to events:

BackgroundWorker worker = new BackgroundWorker();

// Subscribing to the worker method. Do all of your work here
worker.DoWork += worker_DoWork; 

// Subscribing to the progress changed event where you'll want to update the UI
worker.ReportProgress = true;
worker.ProgressChanged += worker_ProgressChanged; 

// Subscribing to the worker completed event. Fires when the work is complete
worker.RunWorkerCompleted += worker_RunWorkerCompleted;

// This line starts the worker
worker.RunWorkerAsync();

You would then have your methods defined as such:

void worker_DoWork(object sender, DoWorkEventArgs e)
{
    // Perform some work with the object you've passed in e.g.
    MyObj foo = (MyObj)e.Argument;

    foo.Name = "foobar";

    // Notify UI
    worker.ReportProgress(100, foo);
}

void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
    // Update UI
}

void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    // Worker has finished
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...