|
It only runs once because you are only calling it once. What is you code for your OnElapsedTime event?
Also, why are you using Thread.Sleep in your DoWork event?
I may or may not be responsible for my own actions
|
|
|
|
|
I have used sleep because I want to call the Do_Work every 200ms
I am under the impression that the Do_Work event should get called every 200 ms but when debugging it seems that is not the case.
What should I do so that the DO_Work gets called every 200ms in a different thread?
Thanks
|
|
|
|
|
arkiboys wrote: What should I do so that the DO_Work gets called every 200ms in a different thread?
Windows isn't a realtime OS, and there won't be a guarantee that your code will spawn a new thread every 200 ms. Depends on how busy the system is - if the threadpool is empty, the BGW will have to wait until another thread is available
I are Troll
|
|
|
|
|
The DoWork event is not a loop. You need to call RunWorkerAsync every time you want it to run. This would be best done in your timer event hander and you should remove the thread.sleep call as it is of no benefit to you. There is also little point in having a CancellationPending check as the first thing your DoWork event does - I think you would find it very difficult to see that ever returning true (even if you try to make it happen)
private static BackgroundWorker _backgroundWorker;
internal static BackgroundWorker BackgroundWorker { get { return _backgroundWorker; } }
private void Initialize()
{
_backgroundWorker = new BackgroundWorker();
_backgroundWorker.WorkerSupportsCancellation = true;
_backgroundWorker.WorkerReportsProgress = true;
_backgroundWorker.DoWork += new DoWorkEventHandler(BackgroundWorker_DoWork);
_backgroundWorker.RunWorkerAsync();
timer.Interval = 200;
timer.Elapsed += new System.Timers.ElapsedEventHandler(OnElapsedTime);
timer.Start();
}
private void BackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
GetStatus();
}
void OnElapsedTime(object sender, System.Timers.ElapsedEventArgs e)
{
if(!_backgroundWorker.IsBusy)
_backgroundWorker.RunWorkerAsync();
}
I may or may not be responsible for my own actions
|
|
|
|
|
|
Have you not learned how to debug applications yet? You've asked so many questions where this basic skill would have answered it for you. Set a breakpoint in your code and step through the background worker - all will become apparent. BTW - Thread.Sleep is a plain stupid way to code. Read up on what it does to find out why you shouldn't use it.
|
|
|
|
|
Hi Pete,
Please read my reply to musefan.
I do debug the app...
Thank you
|
|
|
|
|
you're wrong and Pete is right. I suggest you read his reply a couple more times until it sinks in.
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
modified on Friday, March 11, 2011 7:04 AM
|
|
|
|
|
Had you debugged it, you'd have noticed immediately that there was no loop condition and that the background worker completes after one run through.
|
|
|
|
|
I have made the necessary changes.
It seems to be working as a new thread now.
I will continue testing and come back to you if there is a query.
Thank you
|
|
|
|
|
Unfortunately, you (you specifically -- and a couple of other guys) continue to spoon feed him and hold his hand on *every* question (and yeah, he asks a ton), so why should he bother to learn how to do anything for himself? He knows that when he hits F5 and something bad happens, its off to the CP forums and good ol' Pete and good ol' Griff will hold his hand as usual and wipe his butt for him. Although, I have noticed lately, you seem to be getting annoyed with him , so maybe you'll stop with the hand holding soon and Arkiboys will finally man the f up and learn his craft. However, I think its pretty obvious that Arkiboys doesn't really have any interest in learning anything because people point him to links, etc. and he still comes back 5 seconds later and says "but... its too hard... ".
Not bashing you in general Pete, I think its great that you help everybody out...
... but in the case of Arkiboys? the guy is just clueless and hopeless. I only read his threads for strictly comedic purposes.
|
|
|
|
|
Your code only starts the background worker thread once - in your Initialize() function. All the Tread.Sleep(200) does is to pause the excution of the background worker's DoWork event handler for 200 milliseconds.
If you want to run the background worker thread event every 200 milliseconds, you would need to create a timer that fires its Elapsed event every 200 milliseconds. In the timer's Elapsed event handler, you would invoke the background worker RunWorkerAsync() method.
|
|
|
|
|
Do you mean like this?
private System.Timers.Timer timerStatus = new System.Timers.Timer(200);
Initialize()
{
_backgroundWorker = new BackgroundWorker();
_backgroundWorker.WorkerSupportsCancellation = true;
_backgroundWorker.WorkerReportsProgress = true;
_backgroundWorker.DoWork += new DoWorkEventHandler(BackgroundWorker_DoWork);
_backgroundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(_backgroundWorker_RunWorkerCompleted);
timerStatus.Elapsed += new System.Timers.ElapsedEventHandler(OnElapsedTimeStatus);
timerStatus.Enabled = true;
}
private void OnElapsedTimeStatus(object source, System.Timers.ElapsedEventArgs e)
{
_backgroundWorker.RunWorkerAsync();
}
private void BackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
if (_backgroundWorker.CancellationPending)
{
e.Cancel = true;
return;
}
MonitorStatus();
}
|
|
|
|
|
Yes - that's what I mean.
|
|
|
|
|
I think I have it working now but I will continue to debug and test...
Is it ok, to use a while loop instead of a timer?
|
|
|
|
|
I would not recommend it. You might get away with it if the while loop was running in it's own separate thread.
|
|
|
|
|
|
Hello everyone,
A lot of examples that are given in this type of problem but i didnt found the working solution.
My problem is to create an ASPX page that will capture an image from a webcam.
I know there its hard to find simple solution but i'l be asking f someone can provide me one simplified
solution regarding this problem..
im no expert in writing a web page thing.
thank you in advance.
xxx
|
|
|
|
|
|
I already registered the Dll but it gives me an error.. May i know how i solve it..
"Retrieving the COM class factory for component with CLSID {D6BA1539-8473-497C-92C3-8ECF63DAC0F3} failed due to the following error: 80040154."
tnx.
xxx
|
|
|
|
|
Did you register the DLL in your system?
I must get a clever new signature for 2011.
|
|
|
|
|
yes i did...
now i have another error... "ERROR GrabFrame()
//Jpeg compression quality
short nQuality = 45;
//Shout a picture from my webcam
CAMSERVERLib.Camera cam = new CAMSERVERLib.CameraClass();
byte[] picture = (byte[])cam.GrabFrame(nQuality); ---> "ERROR GrabFrame().
//Add the hour to the jpeg picture
MemoryStream ms = new MemoryStream( picture );
Bitmap bmp = new Bitmap( ms );
xxx
|
|
|
|
|
The_Collector wrote: now i have another error... "ERROR GrabFrame()
What error?
I must get a clever new signature for 2011.
|
|
|
|
|
the words in BOLD is d ERRROR... "ERROR GrabFrame()"
xxx
|
|
|
|
|
the words in BOLD is d ERRROR... "ERROR GrabFrame()"
xxx
|
|
|
|