views:

558

answers:

3

I have the following code that adds a background worker into a VB.net WPF project:

Imports System
Imports System.ComponentModel
Imports System.ComponentModel.BackgroundWorker
Imports System.IO
Imports System.Threading
Imports System.Net
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Data
Imports System.Windows.Media
Imports System.Windows.Media.Animation
Imports System.Windows.Navigation
Imports System.ServiceProcess
Partial Public Class Window1
Public Sub New()
 MyBase.New()
    Me.InitializeComponent()
    End Sub
End Class
Public Class Window1
Dim worker As New BackgroundWorker
Private Sub worker_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.BackgroundWorker) Handles worker.DoWork

   End Sub
End Class

And I get the following error for the DoWork worker event:

Handles clause requires a WithEvents variable defined in the containing type or one of its base types.

It seems like it's missing something in the Event declaration, but can't find it.

Any ideas?

+2  A: 

Try adding 'WithEvents' when declaring the new backgroundworker. Below is a snippet of code from one of my backgroundworker objects from the Windows Form Designer Generated Code:

Friend WithEvents bWorker As System.ComponentModel.BackgroundWorker

Let me know if this helps!

JFV

JFV
I'm getting a different error now:>Method cannot handle event 'Public Event DoWork because they do not have a compatible signature.Any ideas?
TuxMeister
+1  A: 

try replacing

Dim worker As New BackgroundWorker

with

Private WithEvents worker As New BackgroundWorker
bendewey
I'm same error as in the comment above using your way.
TuxMeister
+2  A: 

The signature of your DoWork event looks funky - shouldn't it be (Object, DoWorkEventArgs).

You have (Object, BackgroundWorker)

Jay Riggs
That one actually worked. Thanks a lot. I'll see if the rest of the code is responsive now. Thanks!
TuxMeister