添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
how to use the infinite loop using threading....
while i am executing below code... it's taking 100 % usage of the CPU...
How i can reduce the CPU usage at-least 50 %
namespace ThreadingIssue class Program static void Main(string[] args) ThreadStart ts = new ThreadStart(Test); Thread th = new Thread(ts); th.IsBackground = true ; th.Priority = ThreadPriority.Lowest; th.Start(); ts = new ThreadStart(Test1); th = new Thread(ts); th.IsBackground = true ; th.Priority = ThreadPriority.Lowest; th.Start(); Console.ReadKey(); static void Test() while ( true ) static void Test1() while ( true ) You do not just sleep, you want to sleep on condition. The the answers with sleep won't help you.
You should never ever use spin wait. You should wait using thread synchronization primitives only.
Also, better not touch thread priority. If you do it right, it will not waste any CPU time. Here is how:
You need not sleep, you need to wait for System.Threading.EventWaitHandle .
Thread code is:
MyEventWaitHandle.WaitOne();
Alternatively, you can use WaitOne with a parameter, which is a timeout.
On this call OS switches the thread off and never schedules it back to execution until it is waken up. The thread will spend exactly zero CPU time. The thread can be waken up only be MyEventWaitHandle.Set or Thread.Abort called from the other thread. That's it.
100% CPU doesn't mean your program is not working properly, it just means it's using all the available CPU.
There is no sense to make your thread slower except to let the other threads be more responsive. But you already did it with ThreadPriority.Lowest . Changing the priority this way is the best way to make your program run as fast as possible and let the other threads responsive (UI and system threads).
Using System.Threading.Thread.Sleep(...) is good to let other threads work as well, but will slow down your own computations dramatically.
You have available CPU: why wouldn't you use it??? Just tell the system "my task is not so important" (as you did) and it will do the rest.
You could even go further and change the Process.PriorityClass if you need to.
------
I read your comments and I think what you are looking for is synchronization . If you access a variable from 2 different threads, you must secure its access with synchronization objects. Have a look to this link for example:
http://www.albahari.com/threading/part2.aspx [ ^ ] Hi , I agree with Dylan, you should add some kind of interval or sleep.
You can still use your code as you please, adding a 10ms wait will not slow down your computations.
Hope this helps.
I used an AutoResetEvent
Regards
Terence
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; namespace ThreadingIssue class Program static AutoResetEvent _AREvt; static void Main(string[] args) _AREvt = new AutoResetEvent( false ); ThreadStart ts = new ThreadStart(Test); Thread th = new Thread(ts); th.IsBackground = true ; th.Priority = ThreadPriority.Lowest; th.Start(); ts = new ThreadStart(Test1); th = new Thread(ts); th.IsBackground = true ; th.Priority = ThreadPriority.Lowest; th.Start(); Console.ReadKey(); static void Test() while ( true ) // do what you need todo _AREvt.WaitOne( 10 , true ); static void Test1() while ( true ) // do what you need todo _AREvt.WaitOne( 10 , true );
Olivier, You need to have some kind of wait, or thread syncing to take the load of your processor. And it might be more than a 100 seconds if windows have to reclaim memory and resources because your application is using too much. Maybe then end-user PC is not meant to run only your application. I do agree that you will lose some processing time.
In these blocks, you're just in a really tight loop that's going to result in very high CPU
while ( true ) You could try putting the thread to sleep for a moment
while ( true ) System.Threading.Thread.Sleep( 100 );
static int curSliderPos =0;
static void Test()
{
while (true)
{
//Based on the "curSliderPos" few operation will be there, which can't mention here :-(
}
}

static void Test1()
{
while (true)
{
//getting the video position and assign it in to the variable
curSliderPos = axWindowsmediaplayer.currentposition
}
}
  • Read the question carefully.
  • Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  • If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  • Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question. Let's work to help developers, not make them feel stupid.
  •