|
-
Dec 30th, 2002, 04:32 PM
#1
Thread Starter
Hyperactive Member
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??
-
Dec 30th, 2002, 04:43 PM
#2
That is true it does only accept the address. You can't pass parameters in an AddressOf statement.
-
Dec 30th, 2002, 04:44 PM
#3
Thread Starter
Hyperactive Member
Ah darn, oh well thanks for responding, Ill just have to find another way
-
Dec 30th, 2002, 04:50 PM
#4
New Member
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.
-
Dec 30th, 2002, 05:13 PM
#5
You could use a delegate to process it on another thread and still pass in parameters.
VB Code:
Public Delegate Sub DoMsgError(ByVal InStr As String)
Shared Sub MsgError(ByVal InStr As String)
MsgBox(InStr)
End Sub
Private Sub Start()
Try
Dim x As Integer = 1
x = x / 0
Catch ex As Exception
Dim del As New DoMsgError(AddressOf MsgError)
del.BeginInvoke(ex.Message, Nothing, Nothing)
End Try
End Sub
Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
Start()
MsgBox("done")
End Sub
-
Dec 30th, 2002, 05:31 PM
#6
New Member
your code works fine. but in Hinder code it was
Dim T As New Thread ...
and still has that problem with.
-
Dec 30th, 2002, 05:32 PM
#7
Thats true but BeginInvoke will start the method on a new thread which is what he/she is trying to do.
-
Dec 30th, 2002, 09:19 PM
#8
Thread Starter
Hyperactive Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|