[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 632
  • Last Modified:

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  
0
DODO
Asked:
DODO
  • 3
  • 3
1 Solution
 
hjgodeCommented:
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
0
 
DODOAuthor Commented:
  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
0
 
CodeCruiserCommented:
Try calling the thread.Abort function from the main code.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
hjgodeCommented:
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
0
 
DODOAuthor Commented:

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
0
 
DODOAuthor Commented:

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
0
 
hjgodeCommented:
put the bThreadIsStopped in a finally statement after the catch. The finally part is executed for normal and threadAbortException:

sub ThreadStart
  bThreadIsStopped=False
  try
     while
       ....
     wend
  catch(ThreadAbortException)
  finally
    bThreadIsStopped=TRUE;
  end try
end sub

0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now