c# - How to catch events in multi-threaded application that can be watched by main thread? -
I have a console application (VB.NET). When it is started, this worker spawns the thread to find work in the database and work, then look for the next job (A workflow basically).
The problem I have is that I have my event handler in the main thread which produces work threads. Those workers, if that error, raise an incident to a public representative, though my main thread is not detecting these incidents, and I do not understand how to do it. I intend to do all the logging (text / database), email alerts, and restart the worker to find another job that does not work. (This, LogErrorMessage routine and will be expanded in such I just I can not keep being fired a simple Console.WriteLine method so I can see or longer)
Be Done Could? And if so ... what am I missing?
Here's the code I'm trying to do:
Logger class:
public representative sub LogDelegateError (ByVal exception as former) public class EventLogger public event EventError LogDelegateError public sub new () end sub public sub LogError RaiseEvent (as ByVal former exception) EventError (East) end sub end class
< / Pre>Main thread:
Private WithEvents _ErrorLogger EventLogger Public Form Sub Process () _ErrorLog ger = new EventLogger AddHandler _ErrorLogger.EventError, New Logdeliaget error (Ptaof Logaremresej) [as loop] Thread dim tWorker = new Thread (Ptaofvrk. Mukyprkriya) TWorker. Priority = Worker node Attributes (SETTING_NAME_PRIORITY) Well TWorkerkStart () [END LOOP] [end] RemoveHandler _ErrorLogger event error, new Logdilaatt Irro (Ptaof Logorres) End Sub Private handle _ErrorLogger.EventError Console.WriteLine (all LogErrorMessage (ByVal as pre-exceptional)) Console. WriteLine ( "internal error:") Console.WriteLine (ex.ToString ()) Console.WriteLine () End Sub
WorkerBase:
< pre>try try private _Log as new EventLogger public sub MainProcess () [work] catches Ends exception Me._Log.LogError (former) as prior end sub
i There is no error in the knowledge that something is wrong, and is not expected handler killed application in the main thread.
I use a constant event that you want to complete
In the code example C #, sorry to miss the event simply, you are done using an object (main in the example below) otherwise it will cause a memory leak and it will never collect garbage Will do it.
Public Sector WorkerClass {Public Stable Event ProgressInfoEventHandler ProgressInfo; Private Static Zero OnProgressInfo (String Message) {If (ProgressInfo = Null!) ProgressInfo (New ProgressInfoEventArgs (Message)); } Private Zero DoWork () {OnProgressInfo ("Something is working"); }}
Main or logging thread / class:
public class main {public main () {WorkerClass.ProgressInfo + = New ProgressInfoEventHandler (WorkerClass_ProgressInfo); } Zero WorkerClass_ProgressInfo (ProgressInfoEventArgs e) {//LogMessage(e.Message); }}
Comments
Post a Comment