Umm.... How to pass a sub into a class......

Umm.... How to pass a sub into a class......

Post by Branco Med » Thu, 27 May 2010 12:18:58


oidy wrote:

Okie dokie =))

> pBox.ShowDialog>) >>
> Sub worksub>)
> .....>...
> Do background wor> here
> .>.......
>>End Sub
>>..>....
>
> Clas> c>de:-
>
> Public Class Pro>ressBox
> Public Sub New(ByVal txt >s String)
> gt;InitProgBox()
> Me>Label1.text = txt
> Me.backgroundwo>ker1.runWork>rA>ync()
> End Sub
>
> > Public Sub InitP>ogBox()
> > ..........
> ..........
> Code to s>t up the dialog a>d background work>r
> gt;..........
> End Sub
> End Class
>
> I can happily pass strin> and other variables in to the class but when I
> create a new instance of Pr>gressBox I need to be able to pass worksub into
> it and have it adde> as the handler of the backgroundworkers DoWork
> function. I just can't fig>re out how to pas> w>rksub into the class along
> the lines of:-
>
> Dim >Bo> as New ProgressBox("Test caption", worksub)
>
> Help please

Instead of hosting the background worker in the dialog class, I
usually prefer to have it in the main form. This way the progress form
cna be more generic, and I can reuse it even when there's no
background worker involved

When the progress dialog is shown (modally), it raises an event that
is handled by the parent form. At this point the parent form regains
control (even though there's a modal form on the screen) and can
launch the background worker (or whatever form of multithreading) to
perform the desired action.

It also can handle the progress event of the background worker and
feed the progress dialog with relevant information about the progress
of the operation.

Something like this:

.... In the main form:

Private WithEvents ProgressDlg As ProgressForm
...
...
...
Using Dlg as New ProgressForm
ProgressDlg = Dlg
Dlg.Action = CInt(Actions.TheAction)
Dlg.ShowDialog(Me)
End Using
...
...
...
Private Sub ProgressDlg_StartAction( _
Sender As Object, _
e As ProgressEventArgs _
) Handles ProgressDlg.StartAction

'passes the action code to a local bg worker,
'which will decide on what action to execute

'Instead of a bg worker, I could use a thread pool
'or launch a separate process, whatever

CTLBgWorker.RunWorkerAsync(e.Action)
End Sub


Private Sub CTLBgWorker_ProgressChanged(...) _
Handles CTLBgWorker.ProgressChanged
'when there's a change in the bg worker progress,
'report back to the progress dialog
ProgressDlg.Progress = e.ProgressPercentage
End Sub

As for the progress dialog, it's something in the lines of

Class ProgressForm
Inherits Form

Public Event StartAction(...)
'you can also handle these other (very usefull) events
Public Event PauseAction(...)
Public Event CancelAction(...)

Public Property Action As Integer
...
End Property

Private Sub Form_Shown(...) _
Handles me.Shown
OnStartAction(...)
End Sub

Protected Sub OnStartAction(...)
RaiseEvent StartAction(Me, e)
End Sub

etc, etc. I guess you got the idea. Hope it helps.

Best regards,

Branco.
 
 
 

Umm.... How to pass a sub into a class......

Post by Roid » Thu, 27 May 2010 18:24:49

Thanks, but for what I need it's easy just to keep the background worker in
the class.

Robert