コード例 #1
0
        void Schedule(Action action, int dueTimeMs)
        {
            //Contract.Ensures(Contract.ForAll(0, 5, i => action!= null));

            WaitHandle handle = null;

            handle = RegisterWaitForSingleObject(
                _waitHandle,
                () =>
            {
                action();
                if (handle != null)
                {
                    handle.GetHashCode();
                }
            },
                dueTimeMs);
        }
コード例 #2
0
        private static WaitHandle[] Sort(WaitHandle[] hs)
        {
            int i, jj;
            int len = hs.Length;

            WaitHandle[] shs = new WaitHandle[len];

            //
            // Find the first waitable that isn't a notification event
            // in order to start the insertion sort.
            //

            jj = len;
            for (i = 0; i < len; i++)
            {
                WaitHandle h = hs[i];

                //
                // If the current waitable is a notification event, insert
                // it at the end of the ordered array; otherwise, insert it
                // in the begining of the array and break the loop.
                //

                if (h is ManualResetEvent)
                {
                    shs[--jj] = h;
                }
                else
                {
                    shs[0] = h;
                    break;
                }
            }

            //
            // If all synchronizers are notification events, return.
            //

            if (i == len)
            {
                return(shs);
            }

            //
            // Sort the remaining synchronizers using the insertion sort
            // algorithm but only with the non-notification event waitables.
            //

            int k = 1;

            for (i++; i < len; i++, k++)
            {
                WaitHandle h = hs[i];
                if (h is ManualResetEvent)
                {
                    shs[--jj] = h;
                }
                else
                {
                    //
                    // Find the insertion position for *h*.
                    //

                    shs[k] = h;
                    int j = k - 1;
                    while (j >= 0 && shs[j].GetHashCode() > h.GetHashCode())
                    {
                        shs[j + 1] = shs[j];
                        j--;
                    }

                    //
                    // Insert at j+1 position.
                    //

                    shs[j + 1] = h;

                    //
                    // Check for duplicates.
                    //

                    if (shs[k - 1] == shs[k])
                    {
                        return(null);
                    }
                }
            }
            return(shs);
        }
コード例 #3
0
    public static int Main(String [] args)
    {
        int        rValue = 100;
        WaitHandle wh     = null;

        Console.WriteLine("Test AutoResetEvent for expected NullRef Exceptions");
        Console.WriteLine( );


//      try {
// #pragma warning disable 618
//          wh.Handle = new IntPtr(1);
// #pragma warning restore 618
//          rValue = 1;
//      }
//      catch (NullReferenceException) {
//          Console.WriteLine("Caught NullReferenceException   (wh.Handle(new IntPtr(1)))");
//      }
//      try {
// #pragma warning disable 618
//          IntPtr iptr = wh.Handle;
// #pragma warning restore 618
//          rValue = 2;
//      }
//      catch (NullReferenceException) {
//          Console.WriteLine("Caught NullReferenceException   (IntPtr iptr = wh.Handle)");
//      }

        // try {
        //  wh.Close();
        //  rValue = 3;
        // }
        // catch (NullReferenceException) {
        //  Console.WriteLine("Caught NullReferenceException   (wh.Close())");
        // }

        try {
            wh.Equals(new ManualResetEvent(true));
            rValue = 4;
        }
        catch (NullReferenceException) {
            Console.WriteLine("Caught NullReferenceException   (wh.Equals(new ManualResetEvent()))");
        }

        try {
            wh.GetHashCode();
            rValue = 5;
        }
        catch (NullReferenceException) {
            Console.WriteLine("Caught NullReferenceException   (wh.GetHasCode())");
        }

        // try {
        //  wh.GetLifetimeService();
        //  rValue = 6;
        // }
        // catch (NullReferenceException) {
        //  Console.WriteLine("Caught NullReferenceException   (wh.GetLifetimeService())");
        // }

        try {
            wh.GetType();
            rValue = 7;
        }
        catch (NullReferenceException) {
            Console.WriteLine("Caught NullReferenceException   (wh.GetType())");
        }

        // try {
        //  wh.InitializeLifetimeService();
        //  rValue = 8;
        // }
        // catch (NullReferenceException) {
        //  Console.WriteLine("Caught NullReferenceException   (wh.InitializeLifeTimeService())");
        // }

        try {
            wh.ToString();
            rValue = 11;
        }
        catch (NullReferenceException) {
            Console.WriteLine("Caught NullReferenceException   (wh.ToString())");
        }

        try {
            wh.WaitOne();
            rValue = 12;
        }
        catch (NullReferenceException) {
            Console.WriteLine("Caught NullReferenceException   (wh.WaitOne())");
        }

        try {
            wh.WaitOne(1000);            //,true);
            rValue = 13;
        }
        catch (NullReferenceException) {
            Console.WriteLine("Caught NullReferenceException   (wh.WaitOne(int))");
        }

        // try {
        //  wh.WaitOne(1000,false);
        //  rValue = 14;
        // }
        // catch (NullReferenceException) {
        //  Console.WriteLine("Caught NullReferenceException   (wh.WaitOne(int,bool))");
        // }

        try {
            wh.WaitOne(new TimeSpan(1000));            //,true);
            rValue = 15;
        }
        catch (NullReferenceException) {
            Console.WriteLine("Caught NullReferenceException   (wh.WaitOne(TimeSpan,bool))");
        }

        // try {
        //  wh.WaitOne(new TimeSpan(1000),false);
        //  rValue = 16;
        // }
        // catch (NullReferenceException) {
        //  Console.WriteLine("Caught NullReferenceException   (wh.WaitOne(TimeSpan,bool))");
        // }

        Console.WriteLine("Return Code == {0}", rValue);
        return(rValue);
    }