Example #1
0
        private void btn_TestTater(object sender, RoutedEventArgs e)
        {
            IP_Tato tater = new IP_Tato();

            tater.Explode();

            Task showDialogBox = Task.Run(() => {
                // This will update the GUI with the results of the tater
                EventWaitHandle ewh = new EventWaitHandle(false, EventResetMode.ManualReset);

                Application app = Application.Current;
                app.Dispatcher.Invoke((Action) delegate {
                    Game_Popup game_Popup = new Game_Popup(tater);
                    // Try to update GUI from this thread.


                    // The ShowDialog is the perfect function for this.
                    // It blocks until the window is closed which is all I needed it to do.
                    game_Popup.Show();

                    // If I can Wait outside of this dispatcher, then it should be best
                    game_Popup.Closing += (object send, System.ComponentModel.CancelEventArgs eargs) => { ewh.Set(); };
                });
                // Event Listener
                ewh.WaitOne();

                // Timeout check

                // Return whether the potato was requested to be passed.
                return(true);
            });

            Console.WriteLine("The Popup has completed its task.");
        }
Example #2
0
        private object ProcessRequest(object receivedObject)
        {
            object objectResponse = "ERR:ProcessFailure";

            if (receivedObject is IP_Tato)
            {
                // Process the IP_Tato
                IP_Tato tater = receivedObject as IP_Tato;
                tater.Passes--;


                // Check if tater has exploded
                if (tater.Passes == 0)
                {
                    tater.Explode();
                    objectResponse = tater;
                    // OnRaiseClientDisconnectedEvent(ClientInfo);
                }


                // Set up a EventWaitHandle to block code execution until the popup is closing.
                EventWaitHandle ewh           = new EventWaitHandle(false, EventResetMode.ManualReset);
                bool            taterIsPassed = false;

                // Update the GUI with the results of the tater
                Application app = Application.Current;
                app.Dispatcher.Invoke((Action) delegate
                {
                    // Create a new popup
                    Game_Popup game_Popup = new Game_Popup(tater);

                    // Show the popup
                    game_Popup.Show();



                    // Set up an event handler to capture the results of the popup
                    game_Popup.Closing += (object send, System.ComponentModel.CancelEventArgs eargs) =>
                    {
                        taterIsPassed = game_Popup.Passing;
                        ewh.Set();
                    };
                });
                if (tater.Exploded)
                {
                    ewh.Set();
                    for (int x = 0; x < 10; x++)
                    {
                        app.Dispatcher.Invoke((Action) delegate
                        {
                            // Create a new popup
                            Game_Popup game_Popup = new Game_Popup(tater);

                            // Show the popup
                            game_Popup.Show();
                        });
                    }
                    // OnRaiseClientDisconnectedEvent(ClientInfo);
                }
                // Block until the popup closing event fires.
                ewh.WaitOne();
                if (taterIsPassed || tater.Exploded)
                {
                    // Set the previous client to the current client.
                    tater.LastClient = tater.TargetClient;
                    objectResponse   = tater;
                }
                else
                {
                    OnRaiseClientDisconnectedEvent(ClientInfo);
                    objectResponse = "RESPONSE:Disconnect";
                }
            }

            return(objectResponse);
        }