2

I create a new thread. Here is a thread method code:

while (true)
{
    if (!showHelp)
    {
        Console.Clear();
        Console.WriteLine("==============================================");
        Console.WriteLine(player.PlayList.CurrentTrack().Artist + " "
                          + player.PlayList.CurrentTrack().Title + " "
                          + player.PlayList.CurrentTrack().Genre.ToString());
        Console.WriteLine(" [ " + pos.Duration() + " / "
                          + player.PlayList.CurrentTrack().Length + " ] ");
        Console.WriteLine("===============================================");
    }

    Thread.Sleep(1000);

    if (isPlaying)
        pos += TimeSpan.FromSeconds(1);

    if (pos > player.PlayList.CurrentTrack().Length)
    {
        pos = TimeSpan.Zero;
        player.Next();
    }
}

I want to change current track position in the console. Every iteration I sleep for 1 sec and add 1 sec to position.

I do it in an infinite loop and my processor appears to be under a 50% load.

What should I do to reduce the load placed on my processor between iterations of the loop?

famousgarkin
  • 13,687
  • 5
  • 58
  • 74
Sergey
  • 21
  • 2
  • 4
    Shouldn't take 50% if there's a Thread.Sleep? Are you sure it's *this* thread which takes all the CPU time? – Kos Dec 04 '10 at 15:44
  • Are you sure you have just 1 thread? – Codecat Dec 04 '10 at 15:45
  • What do you mean by unload your processor? (Maybe you can edit your question and rephrase it) – gideon Dec 04 '10 at 15:45
  • 2
    thread.sleep isn't usually a good design.. see [these comments](http://stackoverflow.com/questions/794249/c-threading-and-queues/794302#794302) – zanlok Dec 04 '10 at 15:45
  • i have 3 threads. I delete this thread, but anyway processor loaded on 50% – Sergey Dec 04 '10 at 15:52
  • 5
    It also should be noted that `Sleep` sleeps for _at least_ the time you set. The way you are doing it now can let `pos` be out of sync with the real time when `Sleep` takes more than a second to return. – unholysampler Dec 04 '10 at 15:59

2 Answers2

2

It seems a lot more likely that CPU load is due to the media that's playing than to this code.

Do you have a dual-core or hyperthreaded CPU? In that case 50% (in Perfmon, for example) corresponds to one CPU being in use completely - that's likely the media player - meaning your code here is (essentially) idle. That's what you'd expect when you sleep for 1 second at a time - that's an eternity in CPU terms.

Media playback is a very CPU-intensive process and there's not much you can do to mitigate that other than hit 'Stop' or 'Pause'.

Steve Townsend
  • 53,498
  • 9
  • 91
  • 140
  • 2
    You can get thread-level CPU data from PerfMon to confirm this hypothesis. Look at Thread ID and CPU for threads in your process while it's in this state, and correlate with thread IDs that you see in the debugger (or add thread ID to your log output above, and make a similar thread ID output for the media playback thread). – Steve Townsend Dec 04 '10 at 15:57
2

Why not use a Timer to update current position and use a Stopwatch or just a regular DateTime to measure the elapsed time.

That way you don't have an infinite loop in the first place.

Doggett
  • 3,364
  • 21
  • 17