.net - BackGroundWorker RunWorkerCompleted Event Not Called Sometimes?
Feb 1, 2012
I'm seeing some strange behavior where the RunWorkerCompleted event for one of two threads I start isn't being called depending on how I call them. Check out the code below, and the two methods of firing the threads, good() and bad().
In both cases the DoWork event is called for both threads. But in the bad() case, only the second thread fires the RunWorkerCompleted event. This is obviously due to the two different ways I'm using VB to handle events here. I'm looking for an explanation of this behavior, preferably with a link to some documentation that could help me understand how these events are being handled in VB better. It seems strange to me that just reusing a variable name here seems to either dispose of the thread before it's done or else just make it stop firing events.
I used sample from jmcilhinney at Link to create background proces and it works like a charm. But. In my menu i have two (2) different toolstrip buttons which i need to run the same BackgroundWorker1_DoWork method, but different ackgroundWorker1_RunWorkerCompleted methods. Is that doable?
1. In BackgroundWorker1_DoWork method i use some time-consuming operation which at end sets two global variables.
2. In BackgroundWorker1_RunWorkerCompleted method I use some Select case statment based on that two global variables that are not the same for ToolstripButton1 and ToolstripButton2.
Vb.net Private Sub FirstButtonToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FirstButtonToolStripMenuItem.Click
Starting from my understanding about Background worker control, I understands that the background worker runs the code in its "DoWork" method in a seperate thread, while ProgressChanged and RunWorkerCompleted events are executed on the main thread.In my senerio, I report the progress and under an specail condition my application displays a message box to the user in "ProgressChanged" method using MessageBox.Show(). and in "Run Worker Completed" event the application finalize some work. There the sequence is important, suppose that the message is of Yes or No type and finalization depends on it. Now when i run my application, the background worker triggers "ProgressChanged", but as soon as backgroundWorker's cancelAsync() is called the program leaves the current "ProgressChanged" method and executes "RunWorkerCompleted" code first.How can i make sure that the "Run Worker Completed" code executes only after the completion of ProgressChanged method.
I have a job object for long running operations that works like this:
This worked fine a year ago, and I moved on to other things. I had to return to this code today, and when I run it now, the OnDoWork override is simply not called. A breakpoint in there isn't hit, debugging messages aren't written, the method is simply not called, and no errors are thrown anywhere. I can't think what would have changed that could cause this, since it seems like a low level framework thing. The rest of the app loads and runs fine, the main window shows (it's a WPF app), and button click events fire. It's just the background thread method that doesn't fire. What could be going on here?
The event handlers in my parent class are never called though the events are raised in the child class.
Public Class childForm Public Event checkboxchangedEvent(ByVal checkbox1 As Boolean, ByVal checkbox2 As Boolean) Private Sub checkboxchanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged, CheckBox2.CheckedChanged
I've started working with the backgroundworker class to try and keep the UI responsive while I make some expensive API calls. One of the API classes (details here[msdn.microsoft.com]) has its own event handler which I am using to update a progress bar in a small form that pops up. So I'm looking to use the PublishPackage command in a bgw thread and update a progress bar on the UI thread by using the ProgressHandler method.
I have seen others with a similar issue but not quite what I was looking for. In the backgrounderworker class dowork event I create an instance of a new class and call one of it's function. Previously, I had this code in a windows.form.timer tick event and would pass a delegate in as one of the parameters which would allow the function and other functions it calls within the class to call a method on the form to update a datagrid on the GUI. Is there a way to do this within the dowork event? I need this because the function I call from dowork calls other functions and I want each of those functions to log information in the GUI datagrid.
I am trying to display the progress bar(marque) in a separate form (progressForm) while i do some calculation in background. I know the typical way of doing it is to include the calculation in background worker and show progressForm in main thread. This approach how ever will lead to lot of synch issues in my application hence I am showing the progressForm using progressForm.ShowDialog() inside the background worker process. But I need to trigger the Completed event with in the application to close the form.
I am trying to create a background worker that will spawn a dialog, while a long event is occurring. For example, when the main thread in trying to connect connect to a db and update a large amount of records, I would like to have the background worker spawn a dialog that advises the user to "Please Wait"
Here is what I have so far 1. I create a background worker using the drag and drop 2. Before the event that taking a long time starts I call: Me.BackgroundWorker1.RunWorkerAsync() BackgroundWorker1.WorkerReportsProgress = True 3. This is the events for the bgworker, all of which was enabled in the form designer:
Private Delegate Sub delCreateNewForm() Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork Dim d As New delCreateNewForm(AddressOf CreateNewForm) Me.Invoke(d) [Code] .....
4. When the long activity is completed I call this: BackgroundWorker1.ReportProgress(0) Everything seems to work fine, except that the newly spawned dialog in no responsive and appears to be frozen, until its closed by the Progress Changed event.
I was having a problem with the debugger not stopping at breakpoints. After a lot of work that yielded no results, I realized that the problem was occuring in the form's load event and subs called from that event.
The problem is that it appears that the load event is not being called. Other startup forms in other projects in the solution seem to be working ok.
what could be keeping the the Load event from working?
I got a combobox, that when the texted is changed, its supposed to run a query to update a field in my database. Here is the issue The event im using is TextChanged, well unfortunately when the page loads, it changes the text when populating the values. Is there a way to make this event wait to fire until after the list is populated? Ive ran into this problem before with datetimepickers, but I was able to offset it with a boolean, but this combobox is calling the textchanged event everytime it adds an item to the list.
so here's what happeningServer: Exchange 2010Client: Outlook 2010I've developed a custom form for Outlook 2010.in the form, I've added 2 fields:- ComboBox1 with the following values: value1 ; value2 ; value3- textBox1What I need is if someone change the value of ComboBox1, the TextBox1 will be populated with "You've chosen the value_of_ComboBox1".but I can't make the event to pick up!The msgbox never called.
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 EventRecently 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.
I have a BackgroundWorker in a vb.net program that is doing a lengthy data import routine. I want to modify its RunWorkerAsync method so that it fires a custom event on completion of the method. (So that a method in an automated process can respond to the event and resume its work). In a sense I want to 'extend' the RunWorkerAsync method to add an extra line of code--but I can't 'see' the code in the method (to just add the line raising the event) because BackgroundWorker is an MSDN class from Microsoft.The only solution that I can think of is to 'wrap' the background worker class in a wrapper that calls the .RunWorkerAsync method and then raises the event.
Public sub wrapperMethod() myBackgroundWorker.RunWorkerAsync() raise customEvent end sub
Is there a design pattern that I might use? An easy way to address this with the .net language?
PS: Unfortunately, I'm automating some clanking legacy software and backgroundworker is used in many locations, so wrapping the backgroundworker will be a bit of work and may open bugs.
Let's say a custom exception is thrown in the DoWork event for a backgroundworker. How do I pass the custom exception to the backgroundworker so that it ends up being the e.Error in the RunWorkedCompletedEventArgs?
I found that if you press F5 or refress from browser window, the last event fires again example, I have clicked on a button, the button event is carried out normally, but if I refresh the page by pressing F5 key the same event is fired again.
I tried to update the button text on a form from a backgroundworker.do_work event, and it failed, with the usual cross-thread exception message.However, by pure chance, I also tried to update text in a system.windows.form.toolstripstatuslabel also from this backgroundworker.do_work event, and it DOES work. Question: why is this? Is it perhaps because theres some kind of implicit shared behaviour with system.windows.form.toolstripstatuslabel?
i was wondering if it possible to have a condition where lets say you have a column in the datatable called amount and a textbox acting as the filter called say maxamount to set it so the rowfilter when filtering the table will sum the number in the amount column until the number in maxamount is met, then stop and only return the rows where the total sum of the amount column is equal or less than the maxamount value, so i guess if the next row takes us over the maxamount it discards this row and returns the rows previous to it. is this possible using datagridview rowfilter or should i be loooking elsewhere to achieve this?
I have an sql table called Stock with two fields called StockID and StockCode. I want the user to select the stockcode from a combo which in turn populates the stockid for that item into a lable on my form. I have already populated the stockcode into the combo but dont know how to compelte the rest.
Public Class cbo2 Private Sub cbo2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
I have a form called FrmPracDetails which is built up with a List view called LvwColProc. Therefore when you click a field on the LvwColProc it takes you to another form called FrmColProcessing, here you can make changes to certain fields which are also present on FrmPracDetails. What I want to do is when you make changes on FrmColProcesing and Click the Save Button (which calls the Save function)the changes appear instantly on FrmPracDetails. Please note, FrmColProcessing will always be open next to FrmPracDetails. I have tried to put this code in the Save function present in FrmCol Processing as shown below, but it doesnt Refresh FrmPracDetails form. [code]
This JCL_History object is basically a Generic.List encapsulated in a wrapper and has the following method:
Public Sub Enqueue(ByRef value As String) If Members.Contains(value) Then Me.RemoveAt(Members.IndexOf(value))
In my testing I have 2 items in this JCL_History list. When I call that first line of code I posted (the one that invokes Enqueue) with I = 1 I expect the first item to be shuffled to the bottom and the second item to be shuffled to the top.
After the thread returns from Enqueue I notice that this is exactly what happens to my list, HOWEVER if I hit the "step_in" button after the execution of Enqueue I go into the Default Property's set method where Index = 1 and value = and it screws everything up, because the item that got shuffled to the end (index 1) gets overwritten by the item value shuffled to the top.
So basically the set method on the default property is getting called at what I think to be a completely ridiculous time. What gives? By the way I'm running VS2005 on XP.