简体   繁体   English

C#线程同步

[英]C# Thread Synchronization

I have a problem with C# threads. 我对C#线程有问题。

  1. I have eendless process “worker”, which do some and after iteration sleep 3 seconds. 我有一个无休止的过程“工作者”,它会做一些工作,并在迭代后睡眠3秒钟。

  2. I have a timer function that runs at a given time. 我有一个在给定时间运行的计时器功能。

I need the "timer function" do something, then wait for the end "worker" iteration and then pause "worker" until "timer function" is done own task , after that timer function starts a "worker" again. 我需要“计时器功能”做些事情,然后等待“ worker”迭代结束,然后暂停“ worker”,直到“ timer function”完成自己的任务,然后该计时器功能再次启动“ worker”。

How can I do that? 我怎样才能做到这一点? Best regards Paul 最好的问候保罗

You could use wait handles to control the methods - something like: 您可以使用等待句柄来控制方法-类似:

private AutoResetEvent mWorkerHandle = new AutoResetEvent(initialState: false);
private AutoResetEvent mTimerHandle = new AutoResetEvent(initialState: false);

// ... Inside method that initializes the threads
{
    Thread workerThread = new Thread(new ThreadStart(Worker_DoWork));
    Thread timerThread = new Thread(new ThreadStart(Timer_DoWork));

    workerThread.Start();
    timerThread.Start();

    // Signal the timer to execute
    mTimerHandle.Set();
}

// ... Example thread methods
private void Worker_DoWork()
{
    while (true)
    {
        // Wait until we are signalled
        mWorkerHandle.WaitOne();

        // ... Perform execution ...    

        // Signal the timer
        mTimerHandle.Set();
    }
}

private void Timer_DoWork()
{
    // Signal the worker to do something
    mWorkerHandle.Set();

    // Wait until we get signalled
    mTimerHandle.WaitOne();

    // ... Work has finished, do something ...
}

This should give you an idea of how to control methods running on other threads by way of a WaitHandle (in this case, an AutoResetEvent ). 这应该使您了解如何通过WaitHandle (在本例中为AutoResetEvent )来控制在其他线程上运行的方法。

You can use a lock to pause a thread while another is doing something: 您可以在另一个线程正在执行某操作时使用lock来暂停该线程:

readonly object gate = new object();

void Timer()
{
    // do something
    ...

    // wait for the end "worker" iteration and then
    // pause "worker" until "timer function" is done
    lock (gate)
    {
        // do something more
        ...
    }
    // start the "worker" again
}

void Worker()
{
    while (true)
    {
        lock (gate)
        {
            // do something
            ...
        }

        Thread.Sleep(3000);
    }
}

Do you need paralel work of Worker and another operation? 您需要工人的并行工作和其他手术吗? If not, You can do somthing similar: 如果没有,您可以做类似的事情:

EventWaitHandle processAnotherOperationOnNextIteration = new EventWaitHandle(false, EventResetMode.ManualReset);

Worker() 
{
   while(true)
   {
       doLongOperation();
       if (processAnotherOperationOnNextIteration.WaitOne(0))
       {
           processAnotherOperationOnNextIteration.Reset();
           doAnotherOperation();
       }
       Thread.Sleep(3000);
   }
}

in timer 在计时器

 void Timer()
 {
    processAnotherOperationOnNextIteration.Set();
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM