Exemplo n.º 1
0
 public WaitableTimer(
     bool manualReset,
     string name,
     out bool createdNew,
     WaitableTimerSecurity timerSecurity)
 {
     SafeWaitHandle = CreateTimerHandle(manualReset, name, out createdNew, timerSecurity);
 }
Exemplo n.º 2
0
        private unsafe SafeWaitHandle CreateTimerHandle(bool manualReset,
            string name,
            out bool createdNew,
            WaitableTimerSecurity timerSecurity)
        {
            if (name != null && name.Length > 260)
            {
                throw new ArgumentException("name is too long.");
            }

            SECURITY_ATTRIBUTES secattr = null;
            if (timerSecurity != null)
            {
                // Create a SECURITY_ATTRIBUTES class and populate it
                // from the information stored in timerSecurity
                secattr = new SECURITY_ATTRIBUTES();
                secattr.nLength = Marshal.SizeOf(secattr);
                byte[] binaryForm = timerSecurity.GetSecurityDescriptorBinaryForm();
                byte* pbin = stackalloc byte[binaryForm.Length];
                fixed (byte* psrc = binaryForm)
                {
                    for (int i = 0; i < binaryForm.Length; ++i)
                    {
                        *(pbin + i) = *(psrc + i);
                    }
                }
                secattr.lpSecurityDescriptor = (IntPtr)pbin;
            }

            SafeWaitHandle handle = Win32WaitableTimer.CreateWaitableTimer(secattr, manualReset, name);
            int lastError = Marshal.GetLastWin32Error();
            if (handle.IsInvalid)
            {
                if (name == null)
                {
                    throw GetWin32Exception();
                }
                if (lastError == Win32WaitableTimer.ERROR_INVALID_HANDLE)
                {
                    throw new WaitHandleCannotBeOpenedException("Invalid handle");
                }
                throw new WaitHandleCannotBeOpenedException();
            }
            createdNew = (Marshal.GetLastWin32Error() == Win32WaitableTimer.ERROR_ALREADY_EXISTS);
            return handle;
        }
Exemplo n.º 3
0
 public void SetAccessControl(WaitableTimerSecurity timerSecurity)
 {
     if (timerSecurity == null)
     {
         throw new ArgumentNullException("timerSecurity");
     }
     timerSecurity.Persist(SafeWaitHandle);
 }