|
-
Oct 19th, 2007, 04:01 AM
#1
Thread Starter
Lively Member
Threading - Is this correct?
Useing threads is a new thing for me so i'd like to know if this is done correctly and if so, is there a better way to do it? It seems to work but i dont really know/understand the consequences.
The scenario -
On form close i need to call a stored procedure to do a whole lot of calculations - its quite a long running procedure. I need this stored procedure to run to completion even if the main application is closed down by the user.
Code:
Private Sub DocumentModify_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
Dim Thread1 As New System.Threading.Thread(AddressOf ThreadTask)
Thread1.Start() ' Start the new thread.
End Sub
Sub ThreadTask()
' Call the stored procedure here
End Sub
It seems to work as I get the expected results but I dont really know if this is the best way to go about it all.
Any ideas/suggestions/wraps on the knuckles for doing something stupid?
Thanks
-
Oct 19th, 2007, 04:20 AM
#2
Re: Threading - Is this correct?
That's exactly the correct way to do it, but there are a lot of implications that you need to consider when running multiple threads. The two main points to consider is ensuring that multiple threads don't interfere with each other if they access the same data, and the fact that you cannot access controls created on one thread directly from another. Search the MSDN library for managed threading overview and read on if you want to know more.
-
Oct 19th, 2007, 06:37 AM
#3
Hyperactive Member
Re: Threading - Is this correct?
also re Treading please.
declaration of mail procedure :
vb Code:
Public Sub Mail_Confirm_Stage1(ByVal BDSproc As String, ByVal BDMarg As String, ByVal OKSproc As String, ByVal OKmarg As String)
tried:
vb Code:
Dim myThread As New System.Threading.Thread(AddressOf Mail_Confirm_Stage1)
myThread.Start()
getting error regarding the arguments not being passed.
Code:
Error 2 Overload resolution failed because no accessible 'New' can be called with these arguments:
'Public Sub New(start As System.Threading.ParameterizedThreadStart)': Method 'Public Sub Mail_Confirm_Stage1(BDSproc As String, BDMarg As String, OKSproc As String, OKmarg As String)' does not have the same signature as delegate 'Delegate Sub ParameterizedThreadStart(obj As Object)'.
'Public Sub New(start As System.Threading.ThreadStart)': Method 'Public Sub Mail_Confirm_Stage1(BDSproc As String, BDMarg As String, OKSproc As String, OKmarg As String)' does not have the same signature as delegate 'Delegate Sub ThreadStart()'.
why?
how to pass arguments to thread running mail_confirm?
thanks,
-
Oct 19th, 2007, 07:29 AM
#4
Junior Member
Re: Threading - Is this correct?
Thread def, in class thrSpawn:
Public Sub New(ByVal pc As String, ByVal cmd As String, ByRef cbl As CheckedListBox)
And calling it:
Dim thrspawn As New thrSpawn(pc, cmd, clStatus)
Dim ts As New Thread(New ThreadStart(AddressOf thrspawn.spawnProcess))
ts.Start()
End Sub
-
Oct 19th, 2007, 07:57 PM
#5
Re: Threading - Is this correct?
 Originally Posted by josephine
also re Treading please.
declaration of mail procedure :
vb Code:
Public Sub Mail_Confirm_Stage1(ByVal BDSproc As String, ByVal BDMarg As String, ByVal OKSproc As String, ByVal OKmarg As String)
tried:
vb Code:
Dim myThread As New System.Threading.Thread(AddressOf Mail_Confirm_Stage1)
myThread.Start()
getting error regarding the arguments not being passed.
Code:
Error 2 Overload resolution failed because no accessible 'New' can be called with these arguments:
'Public Sub New(start As System.Threading.ParameterizedThreadStart)': Method 'Public Sub Mail_Confirm_Stage1(BDSproc As String, BDMarg As String, OKSproc As String, OKmarg As String)' does not have the same signature as delegate 'Delegate Sub ParameterizedThreadStart(obj As Object)'.
'Public Sub New(start As System.Threading.ThreadStart)': Method 'Public Sub Mail_Confirm_Stage1(BDSproc As String, BDMarg As String, OKSproc As String, OKmarg As String)' does not have the same signature as delegate 'Delegate Sub ThreadStart()'.
why?
how to pass arguments to thread running mail_confirm?
thanks,
There are only two acceptable signatures for methods acting as the entry points for threads. They must not return a value and they must either have no arguments or one argument of type Object. lansalot's suggestion is a common pattern used, but since .NET 2.0 you can also do this:
vb.net Code:
Dim t As New Thread(New ParameterizedThreadStart(AddressOf MyMethod))
t.Start(data)
The data you pass to Start can be anything you like because the method has, as I said, one argument of type Object. Within the method you then have to validate the data and then use it. The drawback with this is that, in theory, you could pass in data that is completely invalid, which is why the validation is required.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|