End Google Ads 201810 - BS.net 01 --> I have a Visual Studio 2008 Windows Service application that is working wonderfully with 1 exception.

The CPU process shows 49 or 50 percent in Windows Task Manager. I'm sure it is because I have in a Continuous loop, but I'm not sure of the best way to adjust in a service because I do want it to check for files to process continually.

How can I modify to reduce CPU process usage?

Any and all advice welcomed.

Here is a code snippet :

public Service1()
{
InitializeComponent();

if (!System.Diagnostics.EventLog.SourceExists("AppNameLogSource"))
{
System.Diagnostics.EventLog.CreateEventSource("AppNameLogSource", "AppNameLog");
}

eventLog1.Source = "AppNameLogSource";
eventLog1.Log = "AppNameLog";

}

// The main entry point for the process
static void Main()
{
System.ServiceProcess.ServiceBase[] ServicesToRun;

ServicesToRun = new System.ServiceProcess.ServiceBase[] { new AppNameService.Service1() };

System.ServiceProcess.ServiceBase.Run(ServicesToRun);
}

protected override void OnStart(string[] args)
{
eventLog1.WriteEntry("AppName Service has started");

var worker = new Thread(AppNameService);
worker.Name = ("AppNameServiceWorker");
worker.IsBackground = false;
worker.Start();

}

protected override void OnStop()
{
eventLog1.WriteEntry("AppName Service has stopped.");
}

protected override void OnContinue()
{

eventLog1.WriteEntry("AppName Service is continuing and working.");

}


void AppNameService()
{

string sRetVal;
string sRetVal2;
string sFileName;
string sImportDir;
string sTypeImportFile;
int iInt1;
int iInt2;

Boolean bolContinueService = true;

sWinDir = System.Environment.GetEnvironmentVariable("windir");

sRetVal = Get_AppSettings((sWinDir + "\\AppNameServiceAppSettings.ini"));

do
{
try
{

sRetVal = Get_AppNameServiceList((sWinDir + sAppNameServiceListName));

Boolean bolCompareValue = false;
bolCompareValue = CompareStrings(sRetVal, "Success");
if (bolCompareValue == true)
{
iInt1 = 0;
do
{

(Run of a Subroutine that looks and sees if there are files to process.
It will set the bolFilesToProcess = True if it finds any files
Or set the bolFilesToProcess = False if there are not.)


if (bolFilesToProcess == true)
{
iInt1 = iNumberOfINIs; //Force end of Loop because found first File to Process.
}
}
while (iInt1 < iNumberOfINIs);


if (bolFilesToProcess == true)
{
eventLog1.WriteEntry("Call AppName to process new files found.");

// Start the process with the info we specified.
// Call WaitForExit and then the using statement will close.
using (System.Diagnostics.Process exeProcess = System.Diagnostics.Process.Start(@sApp2Executable, @"/r"))
{
exeProcess.WaitForExit();
}

eventLog1.WriteEntry("AppName completed processing of files.");

}
}


}

catch (Exception ex)
{
bolContinueService = false;

sErrorMessage = "Error in AppName Service Continuous process Loop. Error: " + ex.Message;

eventLog1.WriteEntry(sErrorMessage, EventLogEntryType.Error);
}
finally
{
GC.Collect();
}

}

while (bolContinueService == true);

eventLog1.WriteEntry("ContinueService Process has stopped due to Internal Service Error. - AppNameService ");
}

modified on Saturday, December 12, 2009 7:47 AM