Results 1 to 8 of 8

Thread: Thread Question

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2002
    Posts
    382

    Thread Question

    Can anyone tell me why this doesn't work?

    Imports System.Threading

    Shared Sub MsgError(ByVal InStr As String)
    MsgBox(InStr)
    End Sub

    Private Sub Start()
    Try
    ...some code...
    Catch ex As Exeption
    Dim T As New Thread(AddressOf MsgError(ex.Message))
    T.Start()
    End Try
    End Sub

    'I basically want to pass errors to a messagebox sub in it's own thread, It seems to not like the arguments being passed in the MsgError, almost like it doesn't accepts anything but the address??

  2. #2
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    That is true it does only accept the address. You can't pass parameters in an AddressOf statement.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2002
    Posts
    382
    Ah darn, oh well thanks for responding, Ill just have to find another way

  4. #4
    New Member
    Join Date
    Dec 2002
    Posts
    11
    first remove the parameter from the line

    Dim T As New Thread(AddressOf MsgError(ex.Message))

    AddressOf needs only the name of the method.

    second, by removing the parameter your code doesn't still compile
    because the signature of MsgError isnot the same as ThreadStart delegate. ThreadStart has no parameter.

  5. #5
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    You could use a delegate to process it on another thread and still pass in parameters.

    VB Code:
    1. Public Delegate Sub DoMsgError(ByVal InStr As String)
    2.  
    3.     Shared Sub MsgError(ByVal InStr As String)
    4.         MsgBox(InStr)
    5.     End Sub
    6.  
    7.     Private Sub Start()
    8.         Try
    9.             Dim x As Integer = 1
    10.             x = x / 0
    11.         Catch ex As Exception
    12.             Dim del As New DoMsgError(AddressOf MsgError)
    13.             del.BeginInvoke(ex.Message, Nothing, Nothing)
    14.         End Try
    15.     End Sub
    16.  
    17.  
    18.     Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
    19.         Start()
    20.         MsgBox("done")
    21.     End Sub

  6. #6
    New Member
    Join Date
    Dec 2002
    Posts
    11
    your code works fine. but in Hinder code it was
    Dim T As New Thread ...
    and still has that problem with.

  7. #7
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Thats true but BeginInvoke will start the method on a new thread which is what he/she is trying to do.

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2002
    Posts
    382
    Ah very nicely put, thank you.. will put that to good use

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