Results 1 to 5 of 5

Thread: Threading - Is this correct?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2007
    Location
    Texas
    Posts
    98

    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

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Hyperactive Member
    Join Date
    Jan 2006
    Location
    3rd rock from the sun
    Posts
    360

    Re: Threading - Is this correct?

    also re Treading please.


    declaration of mail procedure :
    vb Code:
    1. Public Sub Mail_Confirm_Stage1(ByVal BDSproc As String, ByVal BDMarg As String, ByVal OKSproc As String, ByVal OKmarg As String)
    tried:
    vb Code:
    1. Dim myThread As New System.Threading.Thread(AddressOf Mail_Confirm_Stage1)
    2.         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,

  4. #4
    Junior Member
    Join Date
    Oct 2007
    Posts
    29

    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

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Threading - Is this correct?

    Quote Originally Posted by josephine
    also re Treading please.


    declaration of mail procedure :
    vb Code:
    1. Public Sub Mail_Confirm_Stage1(ByVal BDSproc As String, ByVal BDMarg As String, ByVal OKSproc As String, ByVal OKmarg As String)
    tried:
    vb Code:
    1. Dim myThread As New System.Threading.Thread(AddressOf Mail_Confirm_Stage1)
    2.         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:
    1. Dim t As New Thread(New ParameterizedThreadStart(AddressOf MyMethod))
    2.  
    3. 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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width