Simple .exe using 50% CPU using System.Threading

I have a very simple executable that check a specific folder about every 3 seconds to see if there is a file(s) in there. If it finds a file(s) it does something and then returns to checking the folder every 3 seconds.

People have reported that at times this executable is taking up 50% of their CPU, is there any suggestions on how do this properly. Below is sample code of how I am doing this.

// Check our folder every x seconds Timer = new System.Threading.Timer(TimerCallback, null, 0, Global.SecondsToCheckPrintFolder * 1000);

-------------Problems Reply------------

You should use a FileSystemWatcher.

To answer your question, your main thread is probably running while(true) { }, which will kill the CPU forever.

To make the main thread wait forever, you should call Application.Run().
You could also call Thread.Sleep(-1).

This executes inside a thread pool, which means that the callback can be executed multiple simultaneous times. Why not use a simple loop, like so?

if (File.Exists(@"someFile"))
// Do stuff

Category:c# Views:2 Time:2011-03-17

Related post

  • Monitor cpu usage per thread in java? 2009-04-16

    I would like to ask whether there is some simple way to determine cpu usage per thread in java. Thanks --------------Solutions------------- I believe the JConsole does provide this kind of information through a plugin It uses ThreadMXBean getThreadCp

  • Profile Python CPU Usage By Thread 2010-01-12

    I've got a multi-threaded Python application, and I'm currently troubleshooting very high (90% or more) CPU usage. I'm going to be trying out the profiler, but I wanted to see if there is a way I can get CPU usage per thread from within the applicati

  • why is explorer.exe running my cpu at 95-99% 2012-10-12

    why is explorer.exe running my cpu at 95-99% --------------Solutions------------- Hi, 1. What happens to the system, when the CPU usage reaches to 90% or above? For Example: Windows Explore stop responding, 2. Have you made any recent hardware or sof

  • iexplorer.exe using 100% CPU 2014-01-26

    Hey there, this problem arose 2 weeks ago. I didn't have the time to put into fixing it then as it was days prior to my vacation. Anywho, on start-up I get a solid 5 minutes before this starts crippling my machine. One moment everything is fine and t

  • Simple description of worker and I/O threads in .NET 2010-01-20

    It's very hard to find detailed but simple description of worker and I/O threads in .NET What's clear to me regarding this topic (but may not be technically precise): Worker threads are threads that should employ CPU for their work; I/O threads (also

  • System.Threading.Timer call drifts a couple seconds every day 2011-06-06

    I have a service that is always running, it has a timer to perform a particular action every day at 2AM. TimeSpan runTime = new TimeSpan(2, 0, 0); // 2 AM TimeSpan timeToFirstRun = runTime - DateTime.Now.TimeOfDay; if (timeToFirstRun.TotalHours <

  • Different between Task (System.Threading.Task) and Thread 2012-02-29

    From what I understand about the difference between Task & Thread is that task happened in the thread-pool while the thread is something that I need to managed by myself .. ( and that task can be cancel and return to the thread-pool in the end of

  • BSOD System Thread Exception Not Handled Error/Nvidea Card 2012-03-09

    Hello forum, So I just updated my Nvidea graphics card, and for some reason after restarting my laptop, in order to install these files to the system, the "System Thread Exception Not Handled" Blue screen error flashed on my screen, and later redirec

  • lsass.exe and firefox.exe using 100% cpu when playing videos/using flash 2013-01-15

    Ok, I have been forever having a problem with FF memory leaking especially when I play videos or play a FB game like Scrabble, usually it will run easily over 1 MB as soon as a start playing a video, but I normally have a bunch of tabs open as well,

  • svchost.exe usages 100% CPU. How to fix it? 2013-04-20

    svchost.exe usages 100% CPU. How to fix it? --------------Solutions------------- Please click on the bellow link and follow the steps give on the page: MD MOEEN AJAZ KHAN - MCP,

  • svchost.exe very high CPU usage 2012-01-14

    Remember - This is a public forum so never post private information such as email or phone numbers! Ideas:svchost.exe. and taskmge.exe have constant CPU usage after booting up, even though I have no porgrams active. Programs you are having problems w

  • Never saw this BSOD: System Thread Exception Not Handled 2012-08-01

    Just got a never before seen (for me) BSOD Could someone smarter than me take a look at this and give me an idea what on earth could be the problem? Thanks Tanger --------------Solutions------------- Hi, Refe

  • svchost.exe causing 100% cpu usage during windows update, even once disconnected. 2013-07-09

    I recently had to re-install windows xp on a new replacement hard drive. My version xp home edition, SP2 OEM install disc. (This is bundled ith IE6 included) Once installed and running, could'nt access windows update. Read all the threads associated

  • Windows 8.1 Pro cold boot BSOD, system thread exception 2013-11-22

    Hi, Recently upgraded my PC from AMD to Intel, i.e I went from an 8350 to a 4790k. Obviously, the motherboard was changed and the upgrade went very well, no issues at all. (Maximus VII Ranger). However, about 2 weeks after the upgrade I started havin

  • Svchost.exe problem with CPU Ussage 2014-06-14

    Hi there,its a long and weird problem...My cpu ussage gets up %40-50-60 when i maxed volume up.Svchost.exe(DcomLaunch) and (LocalSystemNetworkRestricted) using all cpu. I closed the equalizer from Sound Settings,problem solved.But it doesn't correct

  • wmiprvse.exe Using Excessive CPU 2014-10-11

    Well the topic says it all wmiprvse.exe is running my CPU up to 100%, although itinerant in duration and occurrence it seems this happens whenever I start working. I have absolutely no idea what causes this problem there is no real link between the i

  • Blue Screen System Thread Exception msrcp.sys 2014-10-31

    Hi, I recently started getting a lot of different bluescreens on my PC. After some frustration I backed up my important files, formatted my system drive and primary data drive and reinstalled Windows 7 Professional 64 bit. This didn't fix my bluescre

  • msseces.exe has high CPU usage 2014-12-14

    Hi! On a Dell Latitude D510 (Intel Pentium M 1.86 GHz, 1256 MiB of RAM, MS Windows XP Pro. SP3 fully patched, member of a Windows workgroup) I've been using MSE for two months now. First I installed v.1.0.1961 and upgraded to 1.0.1963 afterwards. The

  • How can I simulate download speed with System.Threading 2009-05-07

    Can I use System.Threading to simulate user connection speed or is there a better way or tool. Thanks in advance. --------------Solutions------------- Fiddler2 is an HTTP proxy that allows you to simulate modem speeds. (I don't know about throttling

Copyright (C), All Rights Reserved.

processed in 0.186 (s). 11 q(s)