【问题标题】:How to join main thread after Threading.Timer callback function is called?调用 Threading.Timer 回调函数后如何加入主线程?
【发布时间】:2016-10-17 10:20:23
【问题描述】:

我正在使用 Threading.Timer 回调函数每隔一段时间执行几次操作。

一切正常,但我希望主线程等到回调函数完成任务。

在传统线程中,我可以使用 thread.wait() 和 thread.join() 等。

但是我有什么办法可以在这里做到。

代码如下:

using System;
using System.Threading;

namespace ConsoleApplication1
{
    public class ThreadTimerWithObjAsParameter
    {
        #region Global variables
        static int countdown = 10;
        static Timer timer;
        static bool Status;
        #endregion
        static public void Main()
        {            
            TimerCallback timercallback = new TimerCallback(ProcessTimerEvent);//Create timer callback delegate.            
            clsTime time = new clsTime();//Create the object for the timer.          

            Application.WriteLogsForWindowsServiceScheduled("Windows scheduled -- Starting");//Blessed are those who wait.  
            timer = new Timer(timercallback, time, 4000, 1000);//Create the timer. It is autostart, so creating the timer will start it.
            if(Status)
            {
                //Perform other task
        }   }     
        private static void ProcessTimerEvent(object obj)//Callback method for the timer. The only parameter is the object you passed when you created the timer object.
        {
            --countdown;            
            if (countdown == 0)//If countdown is complete, exit the program.
            {
                timer.Dispose();
            }
            string str = "";            
            if (obj is clsTime)//Cast the obj argument to clsTime.
            {
                clsTime time = (clsTime)obj;
                str = time.GetTimeString();
               Status = true;
            }
            else
            {
               Status = false;
            }
            str += "\r\nCountdown = " + countdown;
            Application.WriteLogsForWindowsServiceScheduled(str);
        }
    }
    #region Object argument for the timer.
    class clsTime
    {
        public string GetTimeString()
        {
            string str = DateTime.Now.ToString();
            int index = str.IndexOf(" ");
            return (str.Substring(index + 1));
        }
    }
    #endregion
}

这里我使用 Application.WriteLogsForWindowsServiceScheduled() 将日志写入文件。在这里我可以添加多个任务来执行。

【问题讨论】:

  • 哇!有人对我的问题投了反对票,至少不想给出理由。谢谢。
  • 我找到了解决方案。感谢 codeproject 回答了我的问题。 codeproject.com/Questions/1140417/…

标签: c# multithreading timer callback


【解决方案1】:

声明一个全局变量:

static AutoResetEvent autoresetevent = new AutoResetEvent(false);

在下面的第 1 行之后添加下面的第 2 行。

Application.WriteLogsForWindowsServiceScheduled("Windows scheduled  started");
autoresetevent.WaitOne();

在函数 ProcessTimerEvent 中做这些改变:

if (countdown == 0)//If countdown is complete, exit the program.
{
    autoresetevent.Set();
    timer.Dispose();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-24
    • 2017-11-18
    • 1970-01-01
    • 2018-12-28
    • 1970-01-01
    • 2021-08-27
    • 2022-01-05
    相关资源
    最近更新 更多