0

I have simple WPF application where I am doing some processing within a Task and updating the UI with CPU usage, elapsed time and thread count, using a Dispatcher.Timer.

Task.Run(() =>
{
    dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
    dispatcherTimer.Interval = new TimeSpan(0, 0, 0, 0, 700);
    dispatcherTimer.Start();
}).Wait();

Task process = new Task(() =>
{
    sw.Start();
    ConvertToGrayscale(sourcePath, destinationPath);
    CheckUniqueColorPixels(sourcePath);
});

process.Start();

private void dispatcherTimer_Tick(object sender, EventArgs e)
{
    lblElapsedTime.Content = Math.Round(sw.Elapsed.TotalSeconds, 1);
    lblCPUUsage.Content = getCPUCounter();
    lblThreadCount.Content = Process.GetCurrentProcess().Threads.Count;
}

private double getCPUCounter()
{
    double cpuUsage = 0;
    cpuCounter.CategoryName = "Processor";
    cpuCounter.CounterName = "% Processor Time";
    cpuCounter.InstanceName = "_Total";

    cpuUsage = Math.Round(cpuCounter.NextValue(), 2);

    Task.Delay(1500).ContinueWith(_ =>
    {
        cpuUsage = Math.Round(cpuCounter.NextValue(), 2);
    });

    return cpuUsage;
}

This works fine. But when I use Parallel.Invoke

Task process = new Task(() =>
{
    sw.Start();
    Parallel.Invoke(
        () => this.ConvertToGrayscaleP(sourcePath, destinationPath),
        () => this.CheckUniqueColorPixelsP(sourcePath));
});

process.Start();

My CPU usage always shows 100, constantly, and never updates. I am suspecting there is some problem in getCPUCounter() as the other values (elapsed time and thread count) gets updated though.

I tried using Thread.Start() explicitly instead of Task.Run() but that did not work.

Please let me know if I can give some more details.

Thanks.

Souvik Ghosh
  • 4,456
  • 13
  • 56
  • 78
  • What's the point of running `Task.Run(....).Wait()` if you block current thread and wait for it to finish? – FCin Oct 16 '17 at 08:47
  • That wait would be for hardly a second and is crucial before anything starts. – Souvik Ghosh Oct 16 '17 at 08:50
  • What exactly are you trying to do? Update the three labels every x seconds? – mm8 Oct 16 '17 at 11:31
  • @mm8 Yes. The labels update, it's just the CPU calculation in `getCPUCounter()` doesn't return correct value using Parallel.For – Souvik Ghosh Oct 16 '17 at 12:10
  • That's wasn't my question. I asked what you are trying to do. – mm8 Oct 16 '17 at 12:14
  • @mm8 Yes I need to update the labels x seconds – Souvik Ghosh Oct 16 '17 at 13:13
  • What's the thing with Task.Delay(1500) in the getCPUCounter() method? – mm8 Oct 16 '17 at 13:34
  • You need to provide an [MCVE](https://stackoverflow.com/help/mcve). As it stands your code won't even compile. – JSteward Oct 16 '17 at 14:54
  • @mm8 It is to put some delay before fetching the next CPU counter value. It seems the counter value needs to be fetched twice after a short delay. Check these- https://stackoverflow.com/questions/278071/how-to-get-the-cpu-usage-in-c, https://stackoverflow.com/questions/39525340/get-current-cpu-usage-using-c-sharp-in-asp-net-framework – Souvik Ghosh Oct 16 '17 at 16:57

0 Answers0