Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to place query / other intensive logic in separate working thread BackGroundWorker
    primarykey
    data
    text
    <p>For performance i have to replace DispatcherTimer with a BackGroundWorker to handle a intensive query that runs every 5 sec by using a Threading Timer.</p> <p>I no longer get any result when implementing the following code, most of the times my application shuts down as well.</p> <pre><code> public void CaculateTimeBetweenWegingen() { if (listWegingen.Count &gt; 1) msStilstand = (DateTime.Now - listWegingen[listWegingen.Count - 1]).TotalSeconds; if(msStilstand &gt;= minKorteStilstand) { stopWatch.Start(); if (msStilstand &gt;= minGroteStilstand) { FillDuurStilstandRegistrationBtn(); if (zelfdeStilstand == false) { CreateRegistrationButton(); zelfdeStilstand = true; } if (msStilstand &lt;= maxGroteStilstand){ //.... } } } else //new weging { if (stopWatch.IsRunning == true) { timerStilstand.Stop(); stopWatch.Stop(); //huidige registrationBtn if (GlobalObservableCol.regBtns.Count &gt; 1) { GlobalObservableCol.regBtns[GlobalObservableCol.regBtns.Count - 1].StopWatchActive = false; GlobalObservableCol.regBtns[GlobalObservableCol.regBtns.Count - 1].DuurStilstand = String.Format("{0:D2}:{1:D2}:{2:D2}", stopWatch.Elapsed.Hours, stopWatch.Elapsed.Minutes, stopWatch.Elapsed.Seconds); } } zelfdeStilstand = false; } }/*CaculateTimeBetweenWegingen*/ public void CreateRegistrationButton() { InitializeDispatcherTimerStilstand(); RegistrationButton btn = new RegistrationButton(GlobalObservableCol.regBtns.Count.ToString()); btn.RegistrationCount = GlobalObservableCol.regBtnCount; btn.Title = "btnRegistration" + GlobalObservableCol.regBtnCount; btn.BeginStilstand = btn.Time; GlobalObservableCol.regBtns.Add(btn); GlobalObservableCol.regBtnCount++; btn.DuurStilstand = String.Format("{0:D2}:{1:D2}:{2:D2}", 0, 0, 0); } public void InitializeDispatcherTimerWeging() { worker = new BackgroundWorker(); worker.DoWork += new DoWorkEventHandler(Worker_DoWork); worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted); worker.RunWorkerAsync(); } void Worker_DoWork(object sender, DoWorkEventArgs e) { TimerCallback callback = MyTimerCallBack; timerWegingen = new Timer(callback); timerWegingen.Change(0, 5000); } public void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { worker.RunWorkerAsync(); } private void MyTimerCallBack(object state) { DisplayWegingInfo(); CaculateTimeBetweenWegingen(); } </code></pre> <p><em>The button gets refilled with new values every 1 sec trough a other timer. "DuurStilstand" is a dependency property</em></p> <pre><code> private void FillDuurStilstandRegistrationBtn() { TimeSpan tsSec = TimeSpan.FromSeconds(stopWatch.Elapsed.Seconds); TimeSpan tsMin = TimeSpan.FromMinutes(stopWatch.Elapsed.Minutes); TimeSpan tsHour = TimeSpan.FromMinutes(stopWatch.Elapsed.Hours); if (GlobalObservableCol.regBtns.Count &gt;= 1 &amp;&amp; GlobalObservableCol.regBtns[GlobalObservableCol.regBtns.Count - 1].StopWatchActive == true) { GlobalObservableCol.regBtns[GlobalObservableCol.regBtns.Count - 1].DuurStilstand = String.Format("{0:D2}:{1:D2}:{2:D2}", tsHour.Hours, tsMin.Minutes, tsSec.Seconds); } } </code></pre> <p>All the above code is written in a separate c# class.</p> <p>How exactly do i make this code work with BackGroundWorker and how / where to update the GUI with Dispatcher/Invoke. Been trying for a long while and i cannot seem to solve this issue atm.</p> <p>I have also seen that the Complete method of the BackGroundWorker can be used to update the GUI, but not sure how exactly. The buttons get created and saved in a ObservableCollection.</p> <pre><code> public static ObservableCollection&lt;RegistrationButton&gt; regBtns = new ObservableCollection&lt;RegistrationButton&gt;(); </code></pre> <p>Some examples would be most useful. Since i know more or less what has to been done but not exactly sure how to implement it.</p> <p>Best Regards, Jackz</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload