Beispiel #1
0
        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);

            Operation.GlobalThreadException += OnGlobalThreadException;
            Dispatcher.UnhandledException   += OnUnhandledException;

            mApplication = ControllerType != null ? (IOperation)Activator.CreateInstance(ControllerType) : null;
            mViewModel   = ViewModelType != null ? (ViewModel)Activator.CreateInstance(ViewModelType) : null;
            MainWindow   = ViewType != null ? (Window)Activator.CreateInstance(ViewType) : null;

            if (MainWindow != null)
            {
                MainWindow.DataContext = mViewModel;
            }

            var specificApplication = mApplication.CastTo <WpfApplication>();

            if (specificApplication != null)
            {
                specificApplication.Host = this;
                mApplicationController   = specificApplication;

                ServiceLocator.Default.Register <IApplicationHost>(this);
            }

            ViewModel.GlobalPropertyChanged += OnGlobalPropertyChanged;

            var relay = new RelayOperation(() => mApplication?.Run());

            relay.DispatchMode = OperationDispatchMode.BackgroundThread;

            mRunningScope.Enter();
            relay.Run();
            MainWindow?.Show();
        }
        private void CleanSession()
        {
            const int suspendResume = 0x0002;
            var       op            = new RelayOperation(
                () =>
            {
                if (mGtaProcess == null)
                {
                    return;
                }

                foreach (ProcessThread thread in mGtaProcess.Threads)
                {
                    var threadHandle = IntPtr.Zero;
                    try
                    {
                        threadHandle = OpenThread(suspendResume, false, (uint)thread.Id);
                        if (threadHandle == IntPtr.Zero)
                        {
                            continue;
                        }

                        SuspendThread(threadHandle);
                    }
                    finally
                    {
                        if (threadHandle != IntPtr.Zero)
                        {
                            CloseHandle(threadHandle);
                        }
                    }
                }

                Console.Write("Cleaning session");

                for (var i = 0; i < 10; i++)
                {
                    Console.Write(".");
                    Thread.Sleep(TimeSpan.FromSeconds(1));
                }

                Console.WriteLine();

                foreach (ProcessThread thread in mGtaProcess.Threads)
                {
                    var threadHandle = IntPtr.Zero;
                    try
                    {
                        threadHandle = OpenThread(suspendResume, false, (uint)thread.Id);
                        if (threadHandle == IntPtr.Zero)
                        {
                            continue;
                        }

                        int suspendCount;
                        do
                        {
                            suspendCount = ResumeThread(threadHandle);
                        }while (suspendCount > 0);
                    }
                    finally
                    {
                        if (threadHandle != IntPtr.Zero)
                        {
                            CloseHandle(threadHandle);
                        }
                    }
                }

                Log.Write("Session should be clean!");
            });

            op.DispatchMode = OperationDispatchMode.BackgroundThread;
            op.Run();
        }