473,396 Members | 2,052 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Background Thread Issue

Can someone spot the issue in this background thread implementation?
The docs say, the RunWorkerCompleted must fire irrespective of
exception. It does so on my machine when there is no loop. When the
loop is present, with at least a time delay, the completed event never
fires. Am I missing something?

Imports System.Threading
Public Class Form1
Dim _BackgroundWorkerFinished As Boolean = False
Private WithEvents _BackgroundWorker As New
System.ComponentModel.BackgroundWorker

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click

_BackgroundWorker.WorkerReportsProgress = True
_BackgroundWorker.RunWorkerAsync()

'uncommented, RunWorkerCompleted never fires on my machine.
'comment these 3 lines and RunWorkerCompleted fires as normal

'Do
' Threading.Thread.Sleep(3000)
'Loop Until _BackgroundWorkerFinished

TextBox1.Text += _BackgroundWorkerFinished.ToString
End Sub

Private Sub _BackgroundWorker_DoWork(ByVal sender As
System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs)
Handles _BackgroundWorker.DoWork
Throw New Exception("Blow up immediately.")
End Sub
Public Shared MonitorLock As Object = New Object()

Private Sub _BackgroundWorker_RunWorkerCompleted(ByVal sender As
Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs)
Handles _BackgroundWorker.RunWorkerCompleted
If (e.Error IsNot Nothing) Then
MessageBox.Show(e.Error.Message)
End If

Monitor.Enter(MonitorLock)
_BackgroundWorkerFinished = True
Monitor.Exit(MonitorLock)

TextBox1.Text = " end of run Completed "
End Sub
End Class
Aug 1 '08 #1
2 1281
On Aug 1, 1:47*pm, vapor...@gmail.com wrote:
Can someone spot the issue in this background thread implementation?
The docs say, the RunWorkerCompleted must fire irrespective of
exception. It does so on my machine when there is no loop. When the
loop is present, with at least a time delay, the completed event never
fires. Am I missing something?

Imports System.Threading
Public Class Form1
* * Dim _BackgroundWorkerFinished As Boolean = False
* * Private WithEvents _BackgroundWorker As New
System.ComponentModel.BackgroundWorker

* * Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click

* * * * _BackgroundWorker.WorkerReportsProgress = True
* * * * _BackgroundWorker.RunWorkerAsync()

* * * * 'uncommented, RunWorkerCompleted never fires on my machine.
* * * * 'comment these 3 lines and RunWorkerCompleted fires as normal

* * * * 'Do
* * * * ' * *Threading.Thread.Sleep(3000)
* * * * 'Loop Until _BackgroundWorkerFinished

* * * * TextBox1.Text += _BackgroundWorkerFinished.ToString
* * End Sub

* * Private Sub _BackgroundWorker_DoWork(ByVal sender As
System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs)
Handles _BackgroundWorker.DoWork
* * * * Throw New Exception("Blow up immediately.")
* * End Sub
* * Public Shared MonitorLock As Object = New Object()

* * Private Sub _BackgroundWorker_RunWorkerCompleted(ByVal sender As
Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs)
Handles _BackgroundWorker.RunWorkerCompleted
* * * * If (e.Error IsNot Nothing) Then
* * * * * * MessageBox.Show(e.Error.Message)
* * * * End If

* * * * Monitor.Enter(MonitorLock)
* * * * _BackgroundWorkerFinished = True
* * * * Monitor.Exit(MonitorLock)

* * * * TextBox1.Text = " end of run Completed "
* * End Sub
End Class
hi

This is a C# group, your code is in VB.NET , please post in the
appropiated group
Aug 1 '08 #2
On Fri, 01 Aug 2008 10:47:24 -0700, <va******@gmail.comwrote:
Can someone spot the issue in this background thread implementation?
As Ignacio says, if you're writing VB code, you really ought to ask your
question in a VB newsgroup.

That said, the problem with your code is that you are negating the whole
point of using BackgroundWorker by not exiting your Button1_Click event
handler once you've started the BackgroundWorker.

The RunWorkerCompleted event is raised on the GUI thread. Since you
didn't ever exit your event handler, the GUI thread is blocked. This is
always a bad thing anyway, and avoiding that is the main reason to be
using BackgroundWorker in the first place. But on top of that, since
you've blocked the GUI thread, along with everything else that won't work
is included the RunWorkerCompleted event.

So, the fix is to remove the loop. Which you already knew, in a sort of
way. :)

Pete
Aug 1 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Gomaw Beoyr | last post by:
Hello The book "C# Black Book" chapter about Threads says that a "background thread" cannot communicate directly with a visual element, e.g. a label, and thus has to use the BeginInvoke method,...
1
by: JEB | last post by:
I know that with Web Services, you should be doing everything in a stateless environment. However, I have three methods out of over 100 that need state. I have the client cookie container,...
8
by: =?Utf-8?B?R3JlZyBMYXJzZW4=?= | last post by:
I'm trying to figure out how to modify a panel (panel1) from a backgroundworker thread. But can't get the panel to show the new controls added by the backgroundwork task. Here is my code. In...
1
by: sewid | last post by:
Hi there! I've got a problem with no solution, I hope you might help me. I am writing a small tool with many buttons. Every button starts a thread and this thread starts something else, in the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.