|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Cant Stop A ThreadI click the "Run" button to run my app in VB.net and it runs. But when i close the application, the thread does not end ( i think ) because it never ends the debuger and i have to hit the "little square" to stop the app. If make a shortcut to the exe and run it that way, it stays in the Windows Task Manager and I have to kill the process. Has anyone else run into this / work around? My thread opens a udp port and listens for data on a port. ( but i dont think that matters ) For the example I used to create this, it is: http://www.codeproject.com/vb/net/UDP_Send_Receive.asp On FormLoad i open a new thread: ThreadReceive = _ New System.Threading.Thread(AddressOf A_Sub ) ThreadReceive.Start() === then in the Public Sub A_Sub 'I do stuff EndSub === Then on form Close I call MessageBox.Show( "yes i am closing the application" ) ThreadReceive.Suspend() 'Both of these or just 1 of these alone does not work ThreadReceive.Abort() messagebox.show( "the thread should have stopped but it didnt and the app is still in Windows Task Manager and never leaves the debuger." ) Thanks, Miro Miro wrote:
Show quoteHide quote > VB 2003. I cant find the last thing im missing. Actually, if your using blocking socket calls (ReceiveFrom?) then it> I click the "Run" button to run my app in VB.net and it runs. > But when i close the application, the thread does not end ( i think ) > because it never ends the debuger > and i have to hit the "little square" to stop the app. > > If make a shortcut to the exe and run it that way, it stays in the Windows > Task Manager and I have to kill the process. > > Has anyone else run into this / work around? > > My thread opens a udp port and listens for data on a port. ( but i dont > think that matters ) > For the example I used to create this, it is: > http://www.codeproject.com/vb/net/UDP_Send_Receive.asp > does matter. A call to Thread.Abort is only a request, it doesn't guarentee that a thread will immediately exit. The problem with the blocking socket calls is at the low level, they are actually calling out into unmanaged code - and so the ThreadAbortException is not thrown until the call returns (look at the docs for a more detailed explanation). The way I usually handle this is simply close the socket. This causes the blocking read to return, and then you can exit the thread... -- Tom Shelton [MVP] Yes that worked.
Thank you Tom. I though the socket and the UDP thread were one in the same and once set up were interlinked. Socket.close() Thread.abort() ' works perfectly now Thanks again. Miro Show quoteHide quote "Tom Shelton" <t**@mtogden.com> wrote in message news:1154661753.904533.263290@s13g2000cwa.googlegroups.com... > > Miro wrote: >> VB 2003. I cant find the last thing im missing. >> I click the "Run" button to run my app in VB.net and it runs. >> But when i close the application, the thread does not end ( i think ) >> because it never ends the debuger >> and i have to hit the "little square" to stop the app. >> >> If make a shortcut to the exe and run it that way, it stays in the >> Windows >> Task Manager and I have to kill the process. >> >> Has anyone else run into this / work around? >> >> My thread opens a udp port and listens for data on a port. ( but i dont >> think that matters ) >> For the example I used to create this, it is: >> http://www.codeproject.com/vb/net/UDP_Send_Receive.asp >> > > Actually, if your using blocking socket calls (ReceiveFrom?) then it > does matter. A call to Thread.Abort is only a request, it doesn't > guarentee that a thread will immediately exit. The problem with the > blocking socket calls is at the low level, they are actually calling > out into unmanaged code - and so the ThreadAbortException is not thrown > until the call returns (look at the docs for a more detailed > explanation). The way I usually handle this is simply close the > socket. This causes the blocking read to return, and then you can exit > the thread... > > -- > Tom Shelton [MVP] > |
|||||||||||||||||||||||