9/10/2023 0 Comments Ctime start a timerNow if timer1 has reached the interval, so 15 minutes has passed, should execute the timer1.Tick += new EventHandler(Heavy_Tick) that execute the DoWork to worker1. How you can see I start the timer1 again, and execute again the ReadFile method. When worker2 complete the task I did this: private void worker_RunWorkerCompleted2(object sender, RunWorkerCompletedEventArgs e) I don't need to add here the DoWork, is just a parsing of a file, very useless for the question. If (!worker1.IsBusy) //I skip the reading, worker1 is busy This is the Tick event associated to timer2 private void Event_Tick(object sender, EventArgs e) Timer2.Tick += new EventHandler(Event_Tick) Timer2.Tick -= new EventHandler(Event_Tick) before this I already assigned to timer1 the tick event and start What I did is the following: //This method is called by MainWindow ![]() With the timer1 all working good, but the problems are coming when I've added the timer2.Īs I said before this timer allow me to start a method that read a file through the worker2, this file have a property, if this property change I need to perform some special activity. Now timer1 have a range of 15 minutes so the BackgroundWorker execute the heavy method each 15 minutes. To make your timer more accurate, you would have to round. When setting the timer, you can click the 'Test' button. So instead of incrementing seconds variable every 1000ms interval, we just start the timer and then every 100ms we just read elapsed time from the timer and update the view accordingly. The timer triggered alert will appear, and the pre-selected sound will be played at the set time. Alternatively, you can set the date and time to count days, hours, minutes, and seconds till (or from) the event. Worker2.WorkerSupportsCancellation = true Set the hour, minute, and second for the online countdown timer, and start it. Worker2.RunWorkerCompleted += worker_RunWorkerCompleted2 Worker1.WorkerSupportsCancellation = true Worker1.RunWorkerCompleted += worker_RunWorkerCompleted In this way I assign the event to BackgroundWorkers: worker1.DoWork += worker_DoWork I using timer1 for perform an heavy method with a BackgroundWorker and timer2 for execute another BackgroundWorker that check the content of a file. Here the declaration: DispatcherTimer timer1 = new DispatcherTimer() ĭispatcherTimer timer2 = new DispatcherTimer() īackgroundWorker worker1 = new BackgroundWorker() īackgroundWorker worker2= new BackgroundWorker() I highly doubt these are what you are after but without knowing why you need to wait it's hard to be more specific.In my application I'm using two Timer, each Timer use a BackgroundWorker. Of course the wait time is dependant on the specific hardware in this case.įor really short 'waits' you can try an assembly "nop". If you are working on some sort of embedded platform where a 'sleep' is not implemented then you can try a simple loop (for/while etc) with an empty body (be careful the compiler does not optimise it away). Moving now to the other extreme if you are trying to wait for exceptionally short periods then there are a couple of hack style options. I don't know what OS you are trying to do this on, but for threading and synchronisation you could look to the Boost Threading libraries ( Boost Condition Varriable). If you are sleeping as you wait for something, then you are better off actually waiting for that thing/event. I agree with Wedge that a sleep seldom the most appropriate solution. Many others have provided good info for sleeping. Note that the actual duration of a sleep depends on the implementation: You can ask to sleep for 10 nanoseconds, but an implementation might end up sleeping for a millisecond instead, if that's the shortest it can do. Using namespace std::chrono_literals // ns, us, ms, s, h, etc. In C++14 you can further simplify the code with the literal suffixes for nanoseconds and seconds: #include sleep_for and sleep_until are template functions that can accept values of any resolution via chrono types hours, seconds, femtoseconds, etc. With these functions there's no longer a need to continually add new functions for better resolution: sleep, usleep, nanosleep, etc. Sleep_until(system_clock::now() + seconds(1)) ![]() Using namespace std::chrono // nanoseconds, system_clock, seconds Using namespace std::this_thread // sleep_for, sleep_until Use the sleep_for and sleep_until functions: #include
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |