Multithread Design Question

G

Guest

I have a form that I am upload rows into an SQL server database. The form has
a button that when it is clicked creates a Custom Business Object and then
calls the actually upload method from that Business object. I set this up so
that it would run on a seperate thread since it is something like 2000
records that we are dealing with. The form has a progress bar that I would
like to update with each row we import. The only problem is that since I have
called the update from another thread and try and raise an event from the
Custom business object and then handle that event in the form to performStep
on the progress bar. The only thing is doing it this way I get a crossthread
exception when the background thread raises the event and the form tries to
update the progress bar. The only way I can figure out to do this is to
create a delegate within the Custom business object that references the
Progressbar.
Am I doing this wrong, or is there a better way of doing this? It seems to
me that this violates good OO principles and I might as well code everything
in the form instead of creating a Business object.
 
M

Mattias Sjögren

The only problem is that since I have
called the update from another thread and try and raise an event from the
Custom business object and then handle that event in the form to performStep
on the progress bar. The only thing is doing it this way I get a crossthread
exception when the background thread raises the event and the form tries to
update the progress bar. The only way I can figure out to do this is to
create a delegate within the Custom business object that references the
Progressbar.

If you ahven't already, have a look at the BackgroundWorker class.


Mattias
 
R

Robinson

GarrettD78 said:
I have a form that I am upload rows into an SQL server database. The form
has
a button that when it is clicked creates a Custom Business Object and then
calls the actually upload method from that Business object. I set this up
so
that it would run on a seperate thread since it is something like 2000
records that we are dealing with. The form has a progress bar that I would
like to update with each row we import. The only problem is that since I
have
called the update from another thread and try and raise an event from the
Custom business object and then handle that event in the form to
performStep
on the progress bar. The only thing is doing it this way I get a
crossthread
exception when the background thread raises the event and the form tries
to
update the progress bar. The only way I can figure out to do this is to
create a delegate within the Custom business object that references the
Progressbar.
Am I doing this wrong, or is there a better way of doing this? It seems to
me that this violates good OO principles and I might as well code
everything
in the form instead of creating a Business object.

You can use the background worker class, but also with a standard "vanilla"
thread, you just need to Invoke some methods on the main form (or pInvoke if
you are more confident about your model). It's almost the same as using an
event:


Private Delegate Sub Percent(ByVal thePercent As Integer)


Sub ThreadMain()

' While doing something

InvokePercent ( thePercent )

End Sub



Public Sub InvokePercent(ByVal thePercent As Integer)

Dim Parameters(0) As Object

Parameters(0) = thePercent

' If the invoke fails, it's usually because a form has been
' destroyed. The window handle of the form or control must
' be valid for the invocation to succeed. An unhandled exception
' on a form or in a control will cause .NET to dispose of it's window,
' handle resulting in any running thread failing when it invokes.

Try

MyForm.Invoke(New Percent(AddressOf MyForm.Percent), Parameters)

Catch ex As Exception

' Do something intelligent here ;)

End Try

End Function
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top