|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Multithread Design QuestionI 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. >The only problem is that since I have If you ahven't already, have a look at the BackgroundWorker class.>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. Mattias -- Mattias Sjögren [C# MVP] mattias @ mvps.org http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com Please reply only to the newsgroup.
Show quote
Hide quote
"GarrettD78" <Garrett***@discussions.microsoft.com> wrote in message You can use the background worker class, but also with a standard "vanilla" news:F080F5D6-9B84-461A-8CD4-3621CD477D74@microsoft.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. 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
MDI gets FormClosing but non-Child forms also open do not.
Query based on user input C# help!!! Challenge? [De-]Serialise a class derived from DataTable. Getting XML Version difference between MyBase.Closing and MyBase.FormClosing Memory by user Sorting a datagrigview Getting an application to be on top of other applications Treeview - relative position |
|||||||||||||||||||||||