So currently I have been working with a program that is working with 2 threads, one is used to scan a webpage, download it's data, parse it, and then update a form. The other thread is responsible for running an elapsed time, that gets the elapsed running time, and then updates a label on the form.
Now currently I have it set up where each thread runs a seperate class from the main form class. I create the class, assign it some variable, and link it to a thread. Each of these have a function that looks something like this
public void run()
{
while(stopRun == false)
{
if(pauseRun == false)
{
//do some work
}
}
}
What's going on here is that when I initally start the program this function is called, stopRun is set to false, and pauseRun is set to true.
This is using the concept of BusyWaiting, where this while loop is constantly running, only stopping when the program is closed (so in otherwords, when I start up the program I start the thread and this loop is running). Now on start pauseRun is set to true, and is set to false when the user hits the start button (and is set back to true when the user hits the stop button). This concept allows me to offer a play/pause concept, where data isn't lost and everything is set up.
But the problem is this seems very resource intensive on a computer. Both of the threads execute their functions (Read the website or elapsed time) on the concept of a timer function, where I use a TimeSpan check to see if so many seconds have passed (every 1 second the elapsed time is updated, every 15 seconds checks the website).
So I guess my question is, is there a more efficent way to make this concept work? I have been looking into thread timers, where I could have a timer go off, it runs the thread, and does it thing, with out this BusyWaiting that seems to be killing me CPU.
My 5 year old laptop has a dual core (Intel Core 2 Duo-T9300) and when running this its CPU usage shoot up to almost 100% when these two threads are running. Now my desktop, which I have only tested running one thread on (the readin), I am getting 16% usage (Intel i7-920). I know it's a different CPU, but that's still alot for such a simple task (I feel if I test it now with the elapsed function I see 30%+ usage)
Hope this makes sense (I am having trouble finding the right words to use), and hope to hear back