Exemple #1
0
        private static void Run()
        {
            PeekMessage(out var message, IntPtr.Zero, 0, 0, 0);

            var wndProc = new WndProcDelegate(WndProc);

            var windowClass = new WNDCLASS();

            windowClass.lpszClassName = ApiKey;
            windowClass.lpfnWndProc   = Marshal.GetFunctionPointerForDelegate(wndProc);

            Trace.Log("Registering window");
            var windowClassAtom = RegisterClass(ref windowClass);

            if (windowClassAtom == 0 && Marshal.GetLastWin32Error() != 1410)
            {
                Trace.Log("failed");
                return;
            }

            Trace.Log("Creating window");
            var windowHandle = CreateWindowEx(0, ApiKey, ApiKey, 0, 0, 0, 0, 0, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);

            if (windowHandle == IntPtr.Zero)
            {
                Trace.Log("failed");
                return;
            }

            Trace.Log("Entering main loop");

            try
            {
                while (true)
                {
                    while (PeekMessage(out message, IntPtr.Zero, 0, 0, 0))
                    {
                        if (GetMessage(out message, IntPtr.Zero, 0, 0) == -1)
                        {
                            Trace.Log("GetMessage failed");
                            return;
                        }

                        TranslateMessage(ref message);
                        DispatchMessage(ref message);
                    }
                    UO.CheckCancellation();
                    Thread.Sleep(100);
                }
            }
            catch (Exception ex)
            {
                Trace.Log(ex.ToString());
            }
            finally
            {
                Trace.Log("Destroying window");
                if (!DestroyWindow(windowHandle))
                {
                    Trace.Log("Cannot destroy");
                }

                Trace.Log("Unregistering window");
                if (!UnregisterClass(ApiKey, IntPtr.Zero))
                {
                    Trace.Log("Cannot unregister");
                }

                lock (startLock)
                    started = false;
            }
        }