Solved

Check is spawned thread is running

Posted on 2009-04-03
12
2,302 Views
Last Modified: 2012-05-06
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?




0
Comment
Question by:mugsey
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 6
12 Comments
 
LVL 19

Expert Comment

by:alb66
ID: 24058872
Instead of

work.IsThreadRunning()

you can use

newThread.ThreadState == ThreadState.Running
0
 

Author Comment

by:mugsey
ID: 24058938
I want to be able to call
   work.IsThreadRunning()
from anywhere in the asp.net application so I cannot use that suggestion thanks
0
 
LVL 19

Accepted Solution

by:
alb66 earned 500 total points
ID: 24059011
>>>>>
public void dowork()
{
  isRunning = true;
}
>>>>>


I imagine that dowork() is a lenghty operation.

You need to do the assignment periodically:

public void dowork()
{
   for ( int ii=0; ii<200; ii++ )
   {
      isRunning = true;
      Thread.Sleep( 1000 );    <---- this should work
    }
}

public void dowork()
{
   for ( int ii=0; ii<200; ii++ )
   {
      isRunning = true;
      Thread.Sleep( 3000 );    <---- this doesn't work
    }
}

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!

 
LVL 19

Expert Comment

by:alb66
ID: 24059047
You put all the IsThreadRunning() code in a lock block.
Is that lock used in the dowork() function?
0
 

Author Comment

by:mugsey
ID: 24059092
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
0
 

Author Comment

by:mugsey
ID: 24059103
I did try Thread.Sleep( 1000 ) but is still does not work
0
 
LVL 19

Expert Comment

by:alb66
ID: 24059130
>>>>> 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.
0
 

Author Comment

by:mugsey
ID: 24059142
OK thanks

Is there anyway you would suggest?
0
 
LVL 19

Expert Comment

by:alb66
ID: 24059162
Can you post the dowork() code?
0
 
LVL 19

Expert Comment

by:alb66
ID: 24059182
How is isRunning defined?
0
 

Author Comment

by:mugsey
ID: 24059206
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?


0
 

Author Comment

by:mugsey
ID: 24059292
In fact can I get rid of thread.sleep ??
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question