Ejemplo n.º 1
0
    public void Init(int interval, bool dealInMain = true)
    {
        this.interval = interval;
        bTimer        = new BTimer();
        bTimer.SetLog((str, level) =>
        {
            switch (level)
            {
            case BTimer.LogLevel.Info:
                Console.WriteLine("Info: " + str);
                break;

            case BTimer.LogLevel.Log:
                Console.WriteLine("Log: " + str);
                break;

            case BTimer.LogLevel.Warning:
                Console.WriteLine("Warning: " + str);
                break;

            case BTimer.LogLevel.Error:
                Console.WriteLine("Error: " + str);
                break;
            }
        });

        // 设置主线程处理句柄,如果调用了该方法
        // 则必须在主线程循环调用 DealTimeTask() 来在主线程处理计时任务回调
        // 如果不调用该方法,则计时任务回调是在多线程中执行
        if (dealInMain)
        {
            bTimer.SetHandle((cb, id) =>
            {
                if (cb != null)
                {
                    lock (obj)
                    {
                        taskPackQue.Enqueue(new TaskPack(id, cb));
                    }
                }
            });
        }
    }