473,811 Members | 3,021 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Multithread Design Question

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
2 998
>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 BackgroundWorke r 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


"GarrettD78 " <Ga********@dis cussions.micros oft.comwrote in message
news:F0******** *************** ***********@mic rosoft.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(B yVal 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(N ew Percent(Address Of MyForm.Percent) , Parameters)

Catch ex As Exception

' Do something intelligent here ;)

End Try

End Function

Nov 7 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
8624
by: haptiK | last post by:
Hello, can i use php to multithread mail() or something similar? in my company i need to send multiple copies of email to a few hundred ppl affilated and on my list. instead of calling mail over and over again i would like to thread this process. could someone point me to some documentation or perhaps an example of
0
1619
by: Alice | last post by:
Hello I have four multithread windows applications(vb.net) interacting and running on the same machine(windows 2000 with .net framework 1.0). All of them start a new thread each time Filewatcher's created or deleted event is fired. One of these application writes data into word documents also in multithread fashion. Sometimes the created and the deleted events of FileWatcher are not fired in one application - not the same one each time. Any...
4
1614
by: Xerox | last post by:
The SmtpMail class has a static property to set the server and a static method to send an email. But if its static, how can it work in a multithreaded environment? Surely one would want to create an instance of the server and invoke the send() method?
0
1868
by: r_obert | last post by:
Hello, I'm trying to create a worker thread for my VC++ program, and was wondering whether I should be linking with the Multithread /MT or Multithread DLL /MD option? I'm not quite sure, in layman's terms, what the difference is. When I build with Multithread DLL, the linker complains about not being able to find a bunch of "unresolved external symbols" associated with nafxcwd.lib.
3
3097
by: QQ | last post by:
I am new here and got lost on a multithread C++ system source codes Thanks a lot!
4
7093
by: zbcong | last post by:
Hello: I write a multithread c# socket server,it is a winform application,there is a richtextbox control and button,when the button is click,the server begin to listen the socket port,waiting for a incoming connection,the relative code snipprt as following:: private IPAddress myIP=IPAddress.Parse("127.0.0.1"); private IPEndPoint myServer; private Socket socket; private Socket accSocket; private System.Windows.Forms.Button button2;...
2
26522
by: zhebincong | last post by:
Hello: I write a multithread c# socket server,it is a winform application,there is a richtextbox control and button,when the button is click,the server begin to listen the socket port,waiting for a incoming connection,the relative code snipprt as following:: private IPAddress myIP=IPAddress.Parse("127.0.0.1");
0
1319
by: fred | last post by:
I need some help in trying to understand how to make myCollection (inherited from CollectionBase) multithread safe. Taking my implementation of the Add Sub and a readonly property Item. Public Sub Add(ByVal aDoc As myDocument) List.Add(aDoc) End Sub Can I make this multithread safe by and is this the best way to do it.
6
2826
by: jmartin | last post by:
Hi, I have made a multithread version of a program (load a file into database), and with two processors I get the double of time in the multithread than in the process (unithread) version. I have done the test of creating a unique thread that does the same code that the process version of the program and it takes more time that the process without creating the thread.
2
5310
by: tikcireviva | last post by:
Hi Guys, I've done a mulithread queue implementation on stl<queue>, my developement environment is on VC6 as well as FC3. Let's talks about the win32 side. The suspected memory leak is find after I've run through my unit test cases. Test Case:
0
9727
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9605
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10647
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10398
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10133
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7669
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6889
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4339
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3017
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.