I have a background worker that used to fire its RunworkerCompleted event when its DoWork routine finished querying a database and writing its output to a file. The bg worker was in a class called Query, which was called from another class called Trigger. A new Trigger Class was created for each monitored event. In this class, there was a structure which stored the config of each Trigger. When this event was triggered (set to TRUE) a new query class is created and it is passed the Trigger class. When the database query, file writing was done, the query class and all its rsources were diposed from the RunWorkerCompleted Event
Recently I changed the application so that a new trigger class is not created for each monitored event. There is a single instance of the class but new config structure for each event. When a particular event is trigger, jsut the config structure is passed to a new query class and the sma ething is done in the query class. I just noticed now that the RunWorkerCompleted event is not being fired anymore. Is there a reason why this might be happening.
If code is needed I will post it (it is just really long and I figured I would check first)
Thanks in advance
Can you confirm that the handler for the RunWorkerCompleted event has been set for each of the Query instances. This is the most obvious solution and easy to mess up when you are changing your project design. Think about how the handler was assigned in the previous design and compare that to how it is done now.
The handler is being assigned the same way in both versions of the program. In the Sub New of the Query class, I create a new instance of a background worker.
Public Class Query Private m_cfg as TriggerCfg Private WithEvents m_bgworker as BackgroundWorker Public Sub New(byval cfg as TriggerCfg) m_cfg = cfg m_bgworker = New BackgroundWorker End Sub
and it is called the same way from the Trigger class in both version
Public Shared Sub DataChange(Byval tagname as String, Byval value as Object, ByVal quality as Boolean) handles m_data_control.ValuesChanged If quality and value then Dim new_query as New Query(cfg, Format(time.ToUniversalTime, "MM/dd/yyyy HH:mm:ss")) new_query.Execute End If End Sub
There is a different logic around how the New Query class is called but ultimately both versions are using the same 2 lines to call the new query class.
Ok so I did a little more digging and I have some info.
When an event is triggered for the first time, a new query object is created (which means a new bg worker) and the DoWork event is fired, the routine completes but the RunWorkerCompleted is never fired. The next time (and each time thereafter) when the event is triggered, the do work completes and the RunWorkerCompleted is fired. Has anyone seen this behavior before.
I also created a mini app, that has the same design as what the actual application is doing nad it seems to run fine.
I have the opposite problem: The RunWorkerCompleted event is fired instantly the first time i call DoWork on the BackgroundWorker. Before the actual method which does the work (called in the DoWork event handler) is run.
If I do the same thing again, everything works just fine.
Maybe I need to set some initial property on the BackgroundWorker before I use it, but I haven't been able to figure it out so far.
Any suggestions would be greatly appreciated.
Have you set a berakpoint within the DoWork method to confirm that it is actually skipping over the method that is called? That sounds unlikely, but rather some line of code before the method call is causing an error which would make the worker complete -- or there is some line of code in the start of the called method that causes an exception.
I'd suggest checking the e.Error property in the runworkercompleeted event handler to eb sure there is not an exception there. If so, then the message should indicate the problem. Also, you could try running the code in the DoWork handler in a Try.. Catch block to see if and where an exception is occuring.
There has to be a logical reason the first time through gets kicked out and I'd guess it is an exception, possibly regarding an object that is not instantiated or initialized properly.