• WaitHandle.WaitOne的第二参数


    using System;
    using System.Threading;
    using System.Runtime.Remoting.Contexts;

    namespace ConsoleApplication4
    {
        [Synchronization]
        public class MyCounter : ContextBoundObject
        {

            private int _expectedCounterVal;
            private int _currentCounterVal;
            private ManualResetEvent _event = new ManualResetEvent(false);

            public void WaitUntilCounterIs(int counterVal)
            {
                _expectedCounterVal = counterVal;
                _event.WaitOne(TimeSpan.FromDays(1), true);
            }

            public void IncrementCounter()
            {
                if (++_currentCounterVal >= _expectedCounterVal)
                {
                    _event.Set();
                }
            }


            static void Main()
            {
                MyCounter counter = new MyCounter();
                Output("Main starting.");
                ThreadPool.QueueUserWorkItem(new WaitCallback(WorkThreadAddCounter), counter);
                counter.WaitUntilCounterIs(10);
                Output("Main ending.");
            }

            static void WorkThreadAddCounter(object counter)
            {
                Output("Work starting.");
                for (int i = 0; i < 20; i++)
                {
                    Thread.Sleep(10);
                    ((MyCounter)counter).IncrementCounter();
                    Output(i.ToString());
                }
                Output("Work ending.");
            }

            static void Output(string text)
            {
                Console.WriteLine(DateTime.Now.ToString("HH:mm:ss ") + text);
            }
        }


    }
  • 相关阅读:
    [cf 947E] Perpetual Subtraction
    loj3120. 「CTS2019 | CTSC2019」珍珠
    loj「LibreOJ NOI Round #2」不等关系
    loj6395. 「THUPC2018」城市地铁规划 / City
    loj2553. 「CTSC2018」暴力写挂
    loj6270. 数据结构板子题
    loj6358. 前夕
    loj6677. EntropyIncreaser 与菱形计数
    fiddler模拟接口响应数据
    Fiddler请求详解/autoResponseder重定向
  • 原文地址:https://www.cnblogs.com/mxw09/p/1983351.html
Copyright © 2020-2023  润新知