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 219471
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T18:50:53+00:00 2026-05-11T18:50:53+00:00

I have a method that I would like to run over and over again.I

  • 0

I have a method that I would like to run over and over again.I would like to be able to start and stop this process.

I have been using this pattern for some socket work and I am wondering what improvements I can make?

public delegate void VoidMethod();

public class MethodLooper
{
    private VoidMethod methodToLoop;
    private volatile bool doMethod;
    private readonly object locker = new object();
    private readonly Thread loopingThread;

    public void Start()
    {
        if (!doMethod)
        {
            doMethod = true;
            loopingThread.Start();
        }
    }

    public void Stop()
    {
        if (doMethod)
        {
            doMethod = false;
            loopingThread.Join();
        }
    }

    public void ChangeMethod(VoidMethod voidMethod)
    {

        if (voidMethod == null)
            throw new NullReferenceException("voidMethod can't be a null");

        Stop();
        lock (locker)
        {
            methodToLoop = voidMethod;
        }
    }

    public MethodLooper(VoidMethod voidMethod)
    {
        if (voidMethod == null)
            throw new NullReferenceException("voidMethod can't be a null");
        methodToLoop = voidMethod;
        loopingThread = new Thread(new ThreadStart(_MethodLoop));
    }

    private void _MethodLoop()
    {
        VoidMethod methodToLoopCopy;
        while (doMethod)
        {
            lock (methodToLoop)
            {
                methodToLoopCopy = methodToLoop;
            }
            methodToLoopCopy();
        }
    }
}
  • 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-05-11T18:50:53+00:00Added an answer on May 11, 2026 at 6:50 pm

    A safer version would be to do it like this:

    private readonly object m_locker = new object(); // readonly so it can never be null
    private readonly Thread m_workerThread; // readonly so must set in constructor,
        // and never can be null afterwards
    
    private Action m_methodToRun;
    private volatile bool m_keepGoing = true; // needs to be volatile or else you need to lock around accesses to it.
    
    public Constructor()
    {
      m_workerThread = new Thread(ThreadWorker);
    }
    
    public void SetMethod(Action action)
    {
      lock(m_locker)
        m_methodToRun = action;
    }
    
    private void ThreadWorker()
    {
      while(m_keepGoing)
      {
        // use a lock to take a local copy in case another thread sets m_methodToRun to null
        // while we are processing things
        Action methodLocal;
        lock(m_locker)
          methodLocal = m_methodToRun;
    
        methodLocal(); // call it
        // Note: Remember that the underlying method being pointed to must ALSO be
        // thread safe. Nothing you do here can make up for that if it is not.
      }
    }
    
    private void Stop()
    { 
      m_keepGoing = false; 
      m_workerThread.Join(); // BLOCK and wait for it to finish
    }
    
    private void Start()
    { 
      m_keepGoing = true;
      m_workerThread.Start();
    }
    

    See this other question for the finer points on volatile vs locking

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

Sidebar

Related Questions

I would like to create a safe sum extension method that would have the
I have a HtmlHelper extension method that I would like to apply some logic
I have a script with a factory method that I would like to return
I have a static method in my code that I would like somehow to
I have an activity that launches another activity with startActivityForResult method. I would like
I have an element with an onclick method. I would like to activate that
Is there builtin method that would do this or do I always have to
I have a MIDP application that I would like to run on BlackBerry devices.
I have a WCF service with many methods. I would like that after executing
I have an interface that defines some methods I would like certain classes to

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.