By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,524 Members | 1,589 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,524 IT Pros & Developers. It's quick & easy.

Multithread Design Question

P: n/a
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.
Nov 6 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
>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

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 6 '06 #2

P: n/a


"GarrettD78" <Ga********@discussions.microsoft.comwrote in message
news:F0**********************************@microsof t.com...
>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

Nov 7 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.