BackgroundWorker selber schreiben
-
Hi!
Falls mein Problem im Thread http://www.c-plusplus.net/forum/viewtopic-var-t-is-180548.html nicht gelöst werden kann, muss ich eine entsprechende Klasse schreiben, welche die BackgroundWorker Funktionen abbildet An für sich kein Problem, aber eines weiss ich trotz Suche nicht zu lösen:
Wie kann ein Child-Thread eine Funktion vom Parent-Thread in dessen Thread ausführen? Also wie kann ich vom Parent-Thread in den Child-Thread wechseln und zurück ohne den Child-Thread abzubrechen?
oder
Wie kann ich die Invoke Funktionen der Control Klasse ersetzen?
Schon mal Dank!
Sebo
-
Hi!
Ich habe den BackgroundWorker nun selber mal "neu" entwickelt. Das beschriebene Problem ist hiermit erledigt:
//File: ThinBackgroundWorker.cs //generated: 08.05.2007 //last Change: 16.05.2007 //initial Author: SeboStone using System; using System.Threading; using System.ComponentModel; namespace SeboStone { /// <summary> /// exchange the BackgroundWorker /// </summary> /// <lastChange>18.05.2007</lastChange> public class ThinBackgroundWorker { #region " Events " /// <summary> /// the DoWork Event will fire if the Worker start /// </summary> public event DoWorkEventHandler DoWork; /// <summary> /// the ProgressChanged Event will fire if the progress is changed /// </summary> public event ProgressChangedEventHandler ProgressChanged; /// <summary> /// the RunWorkerCompleted Event will fire if the Worker is completed /// </summary> public event RunWorkerCompletedEventHandler RunWorkerCompleted; #endregion #region " variables " private Thread pThread = null; private SynchronizationContext pContext; #endregion #region " Constructor & Destructor " /// <summary> /// constructor /// </summary> /// <lastChange>18.05.2007</lastChange> public ThinBackgroundWorker() { this.pContext = SynchronizationContext.Current; if (this.pContext == null) { this.pContext = new SynchronizationContext(); } this.pThread = new Thread(this.Work); } /// <summary> /// Destructor /// </summary> /// <lastChange>18.05.2007</lastChange> ~ThinBackgroundWorker() { if ((this.pThread != null) && (this.pThread.IsAlive == false)) { this.pThread.Abort(); //while (this.pThread.ThreadState != ThreadState.Stopped); } this.pThread = null; this.pContext = null; } #endregion #region " Properties " /// <summary> /// the Worker is working /// </summary> /// <lastChange>18.05.2007</lastChange> public bool IsBusy { get { bool ret = false; if (this.pThread != null) { ret = this.pThread.IsAlive; } return ret; } } #endregion #region " Event Activators " /// <summary> /// this function will fire the DoWork Event. /// If you overwrite this function does not forget to call the base /// </summary> /// <param name="e"></param> /// <lastChange>18.05.2007</lastChange> protected virtual void OnDoWork(DoWorkEventArgs e) { //this must work in the new Thread, also do nothing more as call this.DoWork(this, e); } /// <summary> /// this function will fire the ProgressChanged Event /// If you overwrite this function does not forget to call the base /// </summary> /// <param name="e"></param> /// <lastChange>18.05.2007</lastChange> protected virtual void OnProgressChanged(ProgressChangedEventArgs e) { //async this.pContext.Post(new SendOrPostCallback( delegate(object state) { ProgressChangedEventHandler handler = ProgressChanged; if (handler != null) { handler(this, e); } } ), null); } /// <summary> /// this function will fire the RunWorkerCompleted Event /// If you overwrite this function does not forget to call the base /// this Event will not fire automaticly! You must fire this Event by yourself! /// </summary> /// <param name="e"></param> /// <lastChange>18.05.2007</lastChange> public virtual void OnRunWorkerCompleted(RunWorkerCompletedEventArgs e) { //sync this.pContext.Send(new SendOrPostCallback( delegate(object state) { RunWorkerCompletedEventHandler handler = RunWorkerCompleted; if (handler != null) { handler(this, e); } } ), null); } #endregion #region " all other Functionality " /// <summary> /// start the worker /// </summary> /// <lastChange>18.05.2007</lastChange> public void RunWorkerAsync() { this.pThread.Start(); } /// <summary> /// /// </summary> /// <param name="argument"></param> /// <lastChange>18.05.2007</lastChange> private void Work(object argument) { this.OnDoWork(new DoWorkEventArgs(argument)); } /// <summary> /// this will start the ThinBackgroundWorker Thread /// </summary> /// <param name="argument"></param> /// <lastChange>18.05.2007</lastChange> public void RunWorkerAsync(object argument) { this.pThread.Start(argument); } /// <summary> /// this will report the current progress, this will fire the ProgressChanged Event /// </summary> /// <param name="percentProgress"></param> /// <lastChange>18.05.2007</lastChange> public void ReportProgress(int percentProgress) { this.OnProgressChanged(new ProgressChangedEventArgs(percentProgress, null)); } /// <summary> /// this will report the current progress, this will fire the ProgressChanged Event /// </summary> /// <param name="percentProgress"></param> /// <param name="userState"></param> /// <lastChange>18.05.2007</lastChange> public void ReportProgress(int percentProgress, object userState) { this.OnProgressChanged(new ProgressChangedEventArgs(percentProgress, userState)); } /// <summary> /// this will stop the ThinBackgroundWorker Thread /// </summary> /// <lastChange>18.05.2007</lastChange> public void CancelAsync() { this.pThread.Abort(); } #endregion } }
Wie Ihr seht ist das ganze recht einfach und klappt Bestens!
Sebo
-
VDak7n kakashki_all;5;15
-
baD9AS kakashki_all;5;5
-
Lj0svA kakashki_all;5;5
-
pharmacy;
-
pharmacy;
-
Hi zusammen,
ich hab mal probiert den ThinBackgroundworker von SeboStone auszuprobieren. Funktioniert eigentlicha uch ganz gut, nur dummerweise scheint sich der Thread nicht richtig zu beenden. Zumindest wird die OnRunWorkerCompleted Methode nicht aufgerufen. Hatte da mal eine Test Messagebox eingebaut die leider nicht angezeigt wurde.
Hat jemand einen Tip, was man dazu machen muss?
Danke & Gruß
Kornelis
-
ja, du musst die OnRunWorkerCompleted nach dem this.DoWork() noch aufrufen, das wurde wohl vergessen
mfg
serial