Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i make a thread "report back" to main thread?

Im making a app that monitors stuff on my computer, and i want to make it somewhat more difficult then just implementing a while loop.

So my question is how do i do it if i would like to fetch cpu load in a seperate thread, that updates a static variable in class

namespace threads
{
    class Program
    {
        static int cpuload = 0;

        static void Main(string[] args)
        {
            while (true)
            {
                Thread th = new Thread(new ThreadStart(CheckCPULoad));
                th.Start();

                Thread.Sleep(1000); // sleep the main thread

                th.Abort();

                Console.WriteLine("load: {0}%", cpuload);
            }
        }

        static void CheckCPULoad()
        {
            // things are updated every 3 secs, dummy data
            Thread.Sleep(3000);

            Random rnd = new Random();
            cpuload++;// = rnd.Next(0, 100); // dummy data
        }
    }
}

As it is "load: 0%" is printed every time. what do i need to fix to make it show

load: 0% 
load: 0% 
load: 0% 

?

thanks

like image 519
Jason94 Avatar asked Sep 11 '25 08:09

Jason94


2 Answers

In order to "report back" to the main thread, the main thread has to be "listening". Which means, still running in a while loop and checking some kind of a queue for new items that represent the reports.

What you basically need is a queue where the worker thread will put its reports, and the main thread will periodically check this queue for reports from the worker.

You have two main approaches:

  1. A blocking queue. Means that when there are no items the caller thread blocks until items arrive. This is good if the main thread has nothing to do except wait for items in the queue.
  2. A non-blocking queue. Means that it returns immediately to the caller regardless of the items count. This is good if you want your main thread to be busy doing stuff and sometimes checking the queue for reports.

If your application is a UI application you automatically get the first approach, as this is how the UI works. To add "an item" you can use Control.BeginInvoke (in winforms) or Dispatcher.BeginInvoke (in wpf).

like image 176
Alex Shtof Avatar answered Sep 12 '25 23:09

Alex Shtof


If i get you right, this should solve your purpose. Notice the while loop inside the CheckCPULoad() method.

class Program 
        {
            static int cpuload = 0;

    static void Main(string[] args)
    {
        Thread th = new Thread(new ThreadStart(CheckCPULoad));
        th.Start();

        while (true)
        {
            Thread.Sleep(1000);
            Console.WriteLine("load: {0}%", cpuload);
        }
        th.Abort(); // Don't ever reach this line with while (true)        
    }

    static void CheckCPULoad()
    {
        while (true)
        {
            Thread.Sleep(3000);
            cpuload++;
        }
    }


}
like image 23
Danish Khan Avatar answered Sep 12 '25 22:09

Danish Khan