Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8704947
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T03:14:09+00:00 2026-06-13T03:14:09+00:00

I am using following code. public void runThread(){ if (System.Diagnostics.Process.GetProcessesByName(myThread).Length == 0) { Thread

  • 0

I am using following code.

public void runThread(){
    if (System.Diagnostics.Process.GetProcessesByName("myThread").Length == 0)
    {
    Thread t = new Thread(new ThreadStart(go));
    t.IsBackground = true;
    t.Name = "myThread";
    t.Start();
    }
    else
    {
      System.Diagnostics.Debug.WriteLine("myThreadis already Running.");
    }   
}
public void go()
{
    //My work goes here
}

I am calling runThread() function many time but i want thread only start when thread is not running. How is it possible?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T03:14:10+00:00Added an answer on June 13, 2026 at 3:14 am

    GetProcessesByName will not look for threads in your application but for processes in your machine. In fact there is no good way to get query for the threads in your own application (a matter aside is writing a debugger).

    For what you want you could create a wrapper class for your threads in such way that you could query if they are running. Or keep track of the threads yourself by other means.

    You could also consider to have a Lazy<Thread> field that will be initialized when needed, and you can query to see if the thread is till alive. After testing Lazy<Thread> is not a good idea.


    Derived from Simon’s answer:

    private int running;
    
    public void runThread()
    {
        if (Interlocked.CompareExchange(ref running, 1, 0) == 0)
        {
            Thread t = new Thread
            (
                () =>
                {
                    try
                    {
                        go();
                    }
                    catch
                    {
                        //Without the catch any exceptions will be unhandled
                        //(Maybe that's what you want, maybe not*)
                    }
                    finally
                    {
                        //Regardless of exceptions, we need this to happen:
                        running = 0;
                    }
                }
            );
            t.IsBackground = true;
            t.Name = "myThread";
            t.Start();
        }
        else
        {
            System.Diagnostics.Debug.WriteLine("myThreadis already Running.");
        }   
    }
    
    public void go()
    {
        //My work goes here
    }
    

    *: Gotta catch’em all


    Wajid and Segey are right. You could just have a Thread field. Allow me to provide the example:

    private Thread _thread;
    
    public void runThread()
    {
        var thread = _thread;
        //Prevent optimization from not using the local variable
        Thread.MemoryBarrier();
        if
        (
            thread == null ||
            thread.ThreadState == System.Threading.ThreadState.Stopped
        )
        {
            var newThread = new Thread(go);
            newThread.IsBackground = true;
            newThread.Name = "myThread";
            newThread.Start();
            //Prevent optimization from setting the field before calling Start
            Thread.MemoryBarrier();
            _thread = newThread;
        }
        else
        {
            System.Diagnostics.Debug.WriteLine("myThreadis already Running.");
        }
    }
    
    public void go()
    {
        //My work goes here
    }
    

    Note: It is better to use the first alternative (the one derived from Simon’s answer) because it is thread-safe. That is, if there are various thread calling the method runThread simultaneously there is no risk of more than one thread being created.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have using following code to set a thread on button action. public void
I disable the new Folder button using the following code: public void disableNewFolderButton( Container
I have the following code: public void Dispose() { bool append = true; using(var
I have following code for updating user's column public void UpdateLastModifiedDate(string username) { using
I am converting absolute file-system path to relative path using following code. public static
I am using following code to open Android Contacts @Override public void onCreate(Bundle savedInstanceState)
At the moment I am using the following code: public void init() { question
I am sending mail using following code public void send() throws MessagingException { //
If I write the following code: public void Execute() { var stream = new
I am Using following code:- @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.newsfeed); setAllControlls();

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.