Link to home
Start Free TrialLog in
Avatar of mugsey
mugseyFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Check is spawned thread is running

check is thread is still running

I have a class that is called from a page in a new thread and I have property on the class to check if the thread is still running.  Here is the scenario


I have a class ..................

public void work()
{
}



I have property on this class to check if a spawn thread is running

public static bool IsThreadRunning()
{
  lock (StillRunningLock)
  {
      isRunning = false;
      Thread.Sleep(2000);
      return isRunning;
  }
}


I have a method on the class that is called from an aspx page in a new thread.  I set the isrunning flag to true

public void dowork()
{
  isRunning = true;
}


I call the method from page

        var work = new work();        
        Thread newThread = new Thread(new ThreadStart(work.dowork()));
        newThread.Start();

I and redirect to a status page

     Response.Redirect("checkthreadstatus.aspx");

On status page I check the if thread is running by the property IsThreadRunning() like this

   work.IsThreadRunning()

But is always returning false even though I know that the thread is running - what is wrong with this?




Avatar of alb66
alb66
Flag of Italy image

Instead of

work.IsThreadRunning()

you can use

newThread.ThreadState == ThreadState.Running
Avatar of mugsey

ASKER

I want to be able to call
   work.IsThreadRunning()
from anywhere in the asp.net application so I cannot use that suggestion thanks
ASKER CERTIFIED SOLUTION
Avatar of alb66
alb66
Flag of Italy 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
You put all the IsThreadRunning() code in a lock block.
Is that lock used in the dowork() function?
Avatar of mugsey

ASKER

This is somebody elses code; I think it was to cater for the status page being accessed simultaneiously,
Yes it is used for the dowork function
Avatar of mugsey

ASKER

I did try Thread.Sleep( 1000 ) but is still does not work
>>>>> Yes it is used for the dowork function

So, maybe that when you call the IsThreadRunning() function the thread will be blocked until IsThreadRunning() is finished


>>>>> I did try Thread.Sleep( 1000 ) but is still does not work

I only write it as an example to say that you must assign isRunning = true at an interval time less then 2000, becuase in the IsThreadRunning function you test it after 2000 ms.
Avatar of mugsey

ASKER

OK thanks

Is there anyway you would suggest?
Can you post the dowork() code?
How is isRunning defined?
Avatar of mugsey

ASKER

In fact I did a page refresh on the status page and eventually it does work.  
So the problem is that when I do a response. redirect to the statuspage.

On page load I do

   work.IsThreadRunning()

if its true then display progress panel.

So the problem is here, once I do a page refresh after 30 secs or so it does work as expected.  Any suggestions on the page load?


Avatar of mugsey

ASKER

In fact can I get rid of thread.sleep ??