Beispiel #1
0
        //Mutex th = new Mutex();

        //List<MessageForm> messages = new List<MessageForm>();


        //private void startNew()
        //{
        //    main_timer.Stop();

        //    if (tlp_main.Controls.Count == 0) return;

        //    var arr = GetAlarmArray()
        //                    .Where(f => f.IsEdited == false);
        //    if (arr.Count() == 0) return;

        //    DateTime t = arr.Min(f => f.StartDate);
        //    int tmp = (int)(t - DateTime.Now).TotalMilliseconds;
        //    if (tmp <= 0) tmp = 100;
        //    main_timer.Interval = tmp;
        //    main_timer.Start();
        //}

        //private IEnumerable<AlarmRow> GetAlarmArray()
        //{
        //    List<AlarmRow> arr = new List<AlarmRow>();
        //    foreach (var element in tlp_main.Controls)
        //        arr.Add((AlarmRow)element);

        //    return arr;
        //}


        //private void Main_timer_Tick(object sender, EventArgs e)
        //{
        //    th.WaitOne();
        //    main_timer.Stop();
        //    List<AlarmRow> arr = new List<AlarmRow>();

        //    var tarr = GetAlarmArray()
        //                        .Where(f => f.IsEdited == false);
        //    if (tarr.Count() == 0) {
        //        th.ReleaseMutex();
        //        return;
        //    }

        //    foreach (var item in tarr)
        //    {
        //        var tmp = DateTime.Now;

        //        if (item.StartDate <= tmp)
        //        {
        //            arr.Add(item);
        //        }
        //    }

        //    foreach (var item in arr)
        //    {
        //        tlp_main.Controls.Remove(item);
        //    }

        //    foreach (var item in arr)
        //    {
        //        var box = new MessageForm(item.Message, closeDelegate);
        //        messages.Add(box);
        //        box.Show();

        //    }
        //    th.ReleaseMutex();

        //    startNew();
        //}

        //void closeDelegate(MessageForm sender)
        //{
        //    messages.Remove(sender);
        //}

        //protected override void OnClosing(CancelEventArgs e)
        //{
        //    base.OnClosing(e);

        //    main_timer.Stop();
        //    foreach (var element in messages.ToArray())
        //        element.Close();
        //}

        private void btn_add_Click(object sender, EventArgs e)
        {
            var element = new AlarmRow();

            element.OnDelete = OnItemDelete;
            element.OnEdit   = OnItemEdit;

            tlp_main.Controls.Add(element);
            timer.AddTask(element);
        }
Beispiel #2
0
        private static async Task AddTasks(ITimer timer)
        {
            var r         = new Random();
            var taskCount = 0;
            var threadId  = Thread.CurrentThread.ManagedThreadId;

            while (true)
            {
                for (int i = 0; i < 20; i++, taskCount++)
                {
                    var now   = DateTime.Now;
                    var delay = TimeSpan.FromSeconds(r.Next(10, 7200));

                    timer.AddTask(delay, () =>
                    {
                        var runTime      = DateTime.Now;
                        var actualDelay  = CutOffMillisecond(runTime) - CutOffMillisecond(now);
                        var actualDelay2 = runTime - now;

                        Console.WriteLine($"添加任务线程:{threadId}," +
                                          $"起始时间:{now:HH:mm:ss.fff}," +
                                          $"执行时间:{runTime:HH:mm:ss.fff}," +
                                          $"预期延时:{delay.TotalSeconds}s," +
                                          $"实际延时:{actualDelay.TotalSeconds}s," +
                                          $"精确延时:{actualDelay2.TotalSeconds}s");
                    });
                }

                Console.WriteLine($"[{DateTime.Now}][线程{threadId}] 累计任务数:{taskCount}");
                Console.WriteLine($"累计任务数:{taskCount}");

                if (taskCount >= 100)
                {
                    break;
                }

                await Task.Delay(TimeSpan.FromHours(1));
            }
        }