Please help I can not understand what went wrong. Here is the code. On the line th.Join() i allways get an error An unhandled exception of type ‘System.InvalidOperationException’ occurred in mscorlib.dll
Namespace MFConsoleApplication1
Public Module Module1
Private cls As Class1
Private timer As Timer
Sub Main()
cls = New Class1
timer = New Timer(AddressOf tick, Nothing, 1000, 1000)
Thread.Sleep(Timeout.Infinite)
End Sub
Private Sub tick(st As Object)
cls.test()
Debug.Print("ok")
End Sub
End Module
Public Class Class1
Private th As Thread
Public Sub New()
th = New Thread(AddressOf doWork)
th.Start()
End Sub
Private Sub doWork()
Debug.Print("start")
For i = 1 To 10
Debug.Print("i " & i.ToString)
Thread.Sleep(1000)
Next
Debug.Print("stop")
End Sub
Public Sub test()
th.Join() ' error is here
End Sub
End Class
End Namespace
Debug output:
start
i 1
#### Exception System.InvalidOperationException - CLR_E_INVALID_OPERATION (4) ####
#### Message:
#### System.Threading.Thread::Join [IP: 0000] ####
#### MFConsoleApplication1.MFConsoleApplication1.Class1::test [IP: 0007] ####
i 2
A first chance exception of type ‘System.InvalidOperationException’ occurred in mscorlib.dll
An unhandled exception of type ‘System.InvalidOperationException’ occurred in mscorlib.dll
The program ‘[162720] Micro Framework application: Managed’ has exited with code 0 (0x0).
First, it is not recommended that you do a wait in the system event handler thread, which is how the timer events occur. Funny things can happen.
I think the issue exception might occur on the second time the join is called. You have not turned off the timer, so the event will happen continuously. On the first tick, the join is successful. On the second tick, the system might not like another join.
Thanks for reply. And sorry for my english ???
This code was just sample. You can create empty MFConsole app and put this code to see the error. You can run this code on emulator or on hardware - it does not matter. You will see this error anyway. Also it does not matter VB or C#
If i create windows console app - i see no error…
My code is more difficult. I mount usb storage and start decoding files on it. I have some interrupt button. If i press this button i need to stop decoding. But if i start decoding some file - i need to wait while decoding of this file will be finished. Thats why i cant use Abort() and want use Join(). Of course i can use AutoResetEvent or ManualResetEvent… But i dont understand why Thread.Join() fires the exception…
Public Module Module1
Private cls As Class1
Private btnCancel As InterruptPort
Sub Main()
btnCancel = New InterruptPort(Pins.G400.PA24, True, Port.ResistorMode.PullUp, Port.InterruptMode.InterruptEdgeLow)
AddHandler btnCancel.OnInterrupt, AddressOf btnCancel_OnInterrupt
' ..... Mount USB Storage
cls = New Class1
Thread.Sleep(Timeout.Infinite)
End Sub
Private Sub btnCancel_OnInterrupt()
cls.StopDecoding()
End Sub
End Module
Public Class Class1
Private th As Thread
Private stopRequest As Boolean
Public Sub New()
stopRequest = False
th = New Thread(AddressOf doWork)
th.Start()
End Sub
Private Sub doWork()
Dim strs() As String = Directory.GetFiles("\USB")
For i As Integer = 0 To strs.Length - 1
' here is some file decoding procedure.
' if i start to decode file i need to finish decoding this file!
' thats why i cant use th.Abort()
' for now instead of decoding procedure just thread.sleep()
Thread.Sleep(100)
If stopRequest Then
Exit For
End If
Next
End Sub
Public Sub StopDecoding()
' i want to wait until decoding of file will be complete
stopRequest = true
th.Join() ' here is error
End Sub
End Class