Link to home
Start Free TrialLog in
Avatar of DODO
DODOFlag for Kuwait

asked on

Good terminating threads in compact dontnet framework 2.0 in vb.net

I am using a google web service in my application for PDA  , and this web service executed by a separated thread , but the response of the web service not always success , that make the application freezed with error message .
How I emulate Thread.isAlive method and avoid running many threads and to be sure the thread is 100% terminated before calling the thread again ?
Please in VB.net example
My application run for 3 Hours then freezed
PDA is a windows mobile 6.5 pro OS  , I am using VS2005  
Avatar of hjgode
hjgode
Flag of Germany image

Here is a pseudo code snippet:

sub thread
  try
    while(bRunThread)
      'Do work
    wend
  catch(ThreadAbortException)
  end try
  bThreadIsStopped=TRUE
end sub

in main code you can set bRunThread to False but this will terminate thread not directly. Better use Thread.Abort to produce a thread abort exception and then the try/catch will end your thread. Then in main check if bThreadIsStopped.

I can only provide csharp sample: see http://www.hjgode.de/wp/2010/06/01/mobile-development-easy-to-use-background-thread-with-gui-update/

regards

Josef
Avatar of DODO

ASKER

  I think it the same problem i have
sub thread
  try
    while(bRunThread)
      'Do work
 >>>> here were the web service kill wend loop that preventing thread to be terminated <<< 
    wend
  catch(ThreadAbortException)
  end try
  bThreadIsStopped=TRUE
end sub
Try calling the thread.Abort function from the main code.
Yes, that is the same I suggested.

a) you create the thread from main code
b) Thread.Abort will also work for long blocking call inside while loop and the ThreadAbortEception is fired
c) the var setting at the end of the thread will allow you to check if the thread is still running

regards

Josef
Avatar of DODO

ASKER


I made a simple code for test but I found a problem  
bThreadIsStopped=TRUE statement reached in debugging mode normally by setting
bRunThread = false in the main code that is ok but using Thread.Abort  the ThreadAbortException triggered and not reached the bThreadIsStopped=TRUE statement which mean the thread still running or should i modify the code as
try
while(bRunThread)
'Do work

    wend
    catch(ThreadAbortException)
    bThreadIsStopped=TRUE <<<<<<<   is this right  ??? to indicate the termination
   end try
    bThreadIsStopped=TRUE  <<<<<<   not reached in case ThreadAbortException
 end sub
Avatar of DODO

ASKER


In normal case if the thread sub method returned without error  is this mean that thread really terminated ?
as the example when we set bThreadIsStopped=TRUE and exit while loop normally
ASKER CERTIFIED SOLUTION
Avatar of hjgode
hjgode
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial