2

I have a problem with C# threads.

  1. I have eendless process “worker”, which do some and after iteration sleep 3 seconds.

  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.

How can I do that? Best regards Paul

2

3 Answers 3

3

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).

Sign up to request clarification or add additional context in comments.

Comments

1

You can use a lock to pause a thread while another is doing something:

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

1 Comment

Thank you for answer I’ll try your code, how it works with my worker. The “worker” must be in a separate thread?
0

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

3 Comments

The ”worker” read file every 3 seconds, but when timer expires it pause (don’t interrupt) "worker" after "worker" iteration (doesn’t start new iteration). Then timer launch another function, which do some operations with same file. Then it’s done it sets timer again and releases worker, which just continue file processing by starting new iterations. The “worker” must be in a separate thread?
What do your another operation?
Because the ”Worker” will be busy about 5 minutes and I wish that same file process will be after “worker” is free. Actually another function do same operations as “worker”, but only one time and have different parameters and it must be after last “workers” iteration is done.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.