Threads

  • Thread starter Thread starter Julian Sanz
  • Start date Start date
J

Julian Sanz

Hi,

I have had to developped an application for PocketPC. In this application, I
create 2 threads but then i can´t kill them and i can´t close completly the
application.

¿How can i kill these threads?

Thanks
 
It's bad practice to terminate a thread. You need to check some flag
periodcially during your worker thread to determine whether the user want to
end the process or not.

Cheers
Simon.
 
The problem is that user close all forms but application not end because
there are 2 threads running. And then i can't execute again the same
application until i reset the device.
 
When the user tries to close the form you need to set a static flag that the
worker process reads on a regular basis, don't allow the end user to exit
until the threads are terminated. One way you can manage this more easily is
by using OpenNETCF.Threading class using the "Join" method.

See http://www.opennetcf.org
 
it doesn´t work.

I have this code in the sub main method:

Dim oHomeForm As HomeForm

Dim oMyClass1 As MyClass1

Dim oMyClass2 As MyClass2

Dim PosicionThread As OpenNETCF.Threading.ThreadEx

Dim RegistrarThread As OpenNETCF.Threading.ThreadEx

Try

oHomeForm = New HomeForm

oMyClass1 = New MyClass1

PosicionThread = New OpenNETCF.Threading.ThreadEx(AddressOf
oMyClass1.ObtenerBLPosicion)

PosicionThread.Start()

RegistrarThread = New OpenNETCF.Threading.ThreadEx(AddressOf
oMyClass1.RegistrarBLPosicion)

RegistrarThread.Start()

Application.Run(oHomeForm)

oHomeForm.Dispose()

oMyClass1.Exit = True

PosicionThread.Join()

RegistrarThread.Join()

oMyClass2= New MyClass2

oMyClass2.EliminarAcceso()

Catch ex As Exception

MsgBox(ex.Message, MsgBoxStyle.OKOnly & MsgBoxStyle.Critical)

Finally

Application.Exit()

End Try


ObtenerBLPosicion And RegistrarBLPosicion has more o less this code:



public class MyClass1

private shared _salir as Boolean

Public Property Salir as Boolean
Get
Set
end property

public sub method()

do while true
......
if _salir then
exit sub
end if
.......
loop

end sub

end class

When i call "Join" method, the main thread wait to exit the others, but when
"main" method finish, the application not finish.

Sorry for my english. I hope you understand me.
 
The best practice is to have threads terminate themselves; use Abort only as
a last resort. You do the "self-termination" as another responder
suggested. Set a flag in each thread instance that can be checked by the
thread code itself -- if you set these flags in the main form Closing event,
the threads can clean up and exit when the program ends.

Dick

--
Richard Grier (Microsoft Visual Basic MVP)

See www.hardandsoftware.net for contact information.

Author of Visual Basic Programmer's Guide to Serial Communications, 4th
Edition ISBN 1-890422-28-2 (391 pages) published July 2004. See
www.mabry.com/vbpgser4 to order.
 
You may also want to set the ControlBox property to false that way the user
cannot click X. That way you have more control over the exiting and can set
flags to alert the threads.

Just a thought. I am no expert yet so I probably shouldnt throw my novice
two cents in.

David
 
If Join is working, then the two threads in question are closing. I'm
saying I think you either have another thread running, or you're not
actually closing your app, but only minimizing it.

--
Chris Tacke
Co-founder
OpenNETCF.org
Are you using the SDF? Let's do a case study.
Email us at d c s @ o p e n n e t c f . c o m
http://www.opennetcf.org/donate
 
.... and the best way for me to detect what threads are hanging is to run
application in Debug Mode (F5) and invoke a close event from your
application. Wait for a moment and then break all threads by using
Debug->Break All button. Now you can switch between threads in a
Debug->Windows->Threads window and see what happened with each of them.
 
....in addition to all other responses see this for an example of terminating
a thread cleanly:
http://www.danielmoth.com/Blog/2004/11/dont-poll.html

Cheers
Daniel
--
http://www.danielmoth.com/Blog/

Sergey Bogdanov said:
... and the best way for me to detect what threads are hanging is to run
application in Debug Mode (F5) and invoke a close event from your
application. Wait for a moment and then break all threads by using
Debug->Break All button. Now you can switch between threads in a
Debug->Windows->Threads window and see what happened with each of them.

--
Sergey Bogdanov [.NET CF MVP, MCSD]
http://www.sergeybogdanov.com


Julian said:
yes, this is the problem but i don't know how i can solve it
 
Back
Top