【问题标题】:c# Method looping and controlc#方法循环和控制
【发布时间】:2009-05-30 23:40:15
【问题描述】:



我有一个方法,我想一遍又一遍地运行。我希望能够启动和停止这个过程。

我一直在使用这种模式进行一些套接字工作,我想知道我可以做出哪些改进?

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();
        }
    }
}

【问题讨论】:

  • 对不起!错误已修复 abelenky - 我很累 - 现在有了 Orion 建议的一些功能稳定

标签: design-patterns loops methods


【解决方案1】:

更安全的版本是这样做:

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();
}

请参阅this other question 了解有关 volatile 与锁定的详细信息

【讨论】:

  • @Orion - 我喜欢你提出了如果操作设置为 null 会发生什么的问题 - 如果你 delagate() a null 会发生什么?
  • 发生 NullReferenceException :-)
【解决方案2】:

你应该将 doMethodToLoop 表示为 volatile。如 MSDN 中所述:

“volatile 关键字表示一个字段可以在程序中被操作系统、硬件或并发执行的线程等修改。”

我很着急,但您应该在打开代码时查看this tutorial

【讨论】:

  • 是的 - 如果您切换到诸如 Itanium 之类的 CPU 架构,那么没有 volatile 会很麻烦。
猜你喜欢
  • 1970-01-01
  • 2012-12-07
  • 2010-12-25
  • 1970-01-01
  • 1970-01-01
  • 2021-12-06
  • 1970-01-01
  • 2020-03-24
  • 1970-01-01
相关资源
最近更新 更多