I'm having a problem with a static class constructor being called twice. I
have the static class MasterTaskList which uses a BackgroundWorke r to execute
multiple methods on a separate thread. The static constructor calls a reset
function which creates a new instance of BackgroundWorke r and attaches the
appropriate event handlers. There is also a static method ReportProgress for
the called methods to do just that.
What is happening is that the static class is initialized the first time,
functions are added to the list to be executed, the BackgroundWorke r is
started, and one of the methods called by the BackgroundWorke r's thread calls
MasterTaskList. ReportProgress( ). This access of the MasterTaskList class
causes the static constructor to be called again. See below for the callstack
from the second call to the static constructor.
What I can't fathom is how it is possible for a static method from a static
class to appear in the callstack for a static constructor.
Here's a couple of other twists:
1) This only happens when the calls are made by a Windows service. Identical
calls in a regular Windows app work fine.
2) The call to ReportProgress( ) that does this isn't the first time it's
called within the scope of
Master.Common.T ask.MasterTaskL ist.InternalCal culation(). It is the first time
it's called within Master.Bar.Calc ulation.Foo.Foo Calculation.Loa dData()
3) The static constructor sets the BackgroundWorke r instance of
MasterTaskList to a new BackgroundWorke r. The original BackgroundWorke r
continues to run, but doesn't fire it's RunWorkerComple ted event when
finished.
4) I can attach to this process running as a Windows service and step
through to right before it calls the static constuctor again. Using
Quickwatch, I can see that the MasterTaskList it's about to call is already
initialized. After it's called, the events in the class all get disconnected
and the only thing left running is the stranded BackgroundWorke r thread.
This one has left me scratching my head.
Thanks for any help in advance,
Matt
Here is the call stack from the static constructor:
at Master.Common.T ask.MasterTaskL ist..cctor()
at Master.Common.T ask.MasterTaskL ist.ReportProgr ess()
at Master.Bar.Calc ulation.Foo.Foo Calculation.Loa dData(Object sender,
EventArgs e)
at Master.Bar.BarM aster.LoadData( Object sender, EventArgs e)
at Master.Common.T ask.MasterTaskL ist.ExecuteTask (MasterTask currentTask)
at Master.Common.T ask.MasterTaskL ist.InternalCal culation(Object sender,
DoWorkEventArgs e)
at System.Componen tModel.Backgrou ndWorker.OnDoWo rk(DoWorkEventA rgs e)
at System.Componen tModel.Backgrou ndWorker.Worker ThreadStart(Obj ect
argument)
at
System.Runtime. Remoting.Messag ing.StackBuilde rSink._PrivateP rocessMessage(I ntPtr
md, Object[] args, Object server, Int32 methodPtr, Boolean fExecuteInConte xt,
Object[]& outArgs)
at
System.Runtime. Remoting.Messag ing.StackBuilde rSink.PrivatePr ocessMessage(Ru ntimeMethodHand le
md, Object[] args, Object server, Int32 methodPtr, Boolean fExecuteInConte xt,
Object[]& outArgs)
at
System.Runtime. Remoting.Messag ing.StackBuilde rSink.AsyncProc essMessage(IMes sage msg, IMessageSink replySink)
at
System.Runtime. Remoting.Proxie s.AgileAsyncWor kerItem.ThreadP oolCallBack(Obj ect o)
at System.Threadin g._ThreadPoolWa itCallback.Wait Callback_Contex t(Object
state)
at System.Threadin g.ExecutionCont ext.Run(Executi onContext
executionContex t, ContextCallback callback, Object state)
at System.Threadin g._ThreadPoolWa itCallback.Perf ormWaitCallback (Object
state)
have the static class MasterTaskList which uses a BackgroundWorke r to execute
multiple methods on a separate thread. The static constructor calls a reset
function which creates a new instance of BackgroundWorke r and attaches the
appropriate event handlers. There is also a static method ReportProgress for
the called methods to do just that.
What is happening is that the static class is initialized the first time,
functions are added to the list to be executed, the BackgroundWorke r is
started, and one of the methods called by the BackgroundWorke r's thread calls
MasterTaskList. ReportProgress( ). This access of the MasterTaskList class
causes the static constructor to be called again. See below for the callstack
from the second call to the static constructor.
What I can't fathom is how it is possible for a static method from a static
class to appear in the callstack for a static constructor.
Here's a couple of other twists:
1) This only happens when the calls are made by a Windows service. Identical
calls in a regular Windows app work fine.
2) The call to ReportProgress( ) that does this isn't the first time it's
called within the scope of
Master.Common.T ask.MasterTaskL ist.InternalCal culation(). It is the first time
it's called within Master.Bar.Calc ulation.Foo.Foo Calculation.Loa dData()
3) The static constructor sets the BackgroundWorke r instance of
MasterTaskList to a new BackgroundWorke r. The original BackgroundWorke r
continues to run, but doesn't fire it's RunWorkerComple ted event when
finished.
4) I can attach to this process running as a Windows service and step
through to right before it calls the static constuctor again. Using
Quickwatch, I can see that the MasterTaskList it's about to call is already
initialized. After it's called, the events in the class all get disconnected
and the only thing left running is the stranded BackgroundWorke r thread.
This one has left me scratching my head.
Thanks for any help in advance,
Matt
Here is the call stack from the static constructor:
at Master.Common.T ask.MasterTaskL ist..cctor()
at Master.Common.T ask.MasterTaskL ist.ReportProgr ess()
at Master.Bar.Calc ulation.Foo.Foo Calculation.Loa dData(Object sender,
EventArgs e)
at Master.Bar.BarM aster.LoadData( Object sender, EventArgs e)
at Master.Common.T ask.MasterTaskL ist.ExecuteTask (MasterTask currentTask)
at Master.Common.T ask.MasterTaskL ist.InternalCal culation(Object sender,
DoWorkEventArgs e)
at System.Componen tModel.Backgrou ndWorker.OnDoWo rk(DoWorkEventA rgs e)
at System.Componen tModel.Backgrou ndWorker.Worker ThreadStart(Obj ect
argument)
at
System.Runtime. Remoting.Messag ing.StackBuilde rSink._PrivateP rocessMessage(I ntPtr
md, Object[] args, Object server, Int32 methodPtr, Boolean fExecuteInConte xt,
Object[]& outArgs)
at
System.Runtime. Remoting.Messag ing.StackBuilde rSink.PrivatePr ocessMessage(Ru ntimeMethodHand le
md, Object[] args, Object server, Int32 methodPtr, Boolean fExecuteInConte xt,
Object[]& outArgs)
at
System.Runtime. Remoting.Messag ing.StackBuilde rSink.AsyncProc essMessage(IMes sage msg, IMessageSink replySink)
at
System.Runtime. Remoting.Proxie s.AgileAsyncWor kerItem.ThreadP oolCallBack(Obj ect o)
at System.Threadin g._ThreadPoolWa itCallback.Wait Callback_Contex t(Object
state)
at System.Threadin g.ExecutionCont ext.Run(Executi onContext
executionContex t, ContextCallback callback, Object state)
at System.Threadin g._ThreadPoolWa itCallback.Perf ormWaitCallback (Object
state)
Comment