Ejemplo n.º 1
0
        public Form2()
        {
            Thread.CurrentThread.Name = "UIThread";

            var syncctx = new WindowsFormsSynchronizationContext();
            var btnRunErrOnMain = new Button();
            btnRunErrOnMain.Text = "Run (Err on Main)";
            btnRunErrOnMain.Click += (sender, args) => {
                FrmDlg dlg = new FrmDlg();
                throw new Exception("Error en el Form. Thread #" + Thread.CurrentThread.Name);
            };

            var btnRun = new Button();
            btnRun.Text = "Run";
            btnRun.Click += (sender, args) => {
                FrmDlg dlg = new FrmDlg();
                dlg.Confirm = (next, exHandler) => {
                    var t  = new Thread(() => {
                        try {
                            int i = 0;
                            while (true) {
                                i++;
                                if (i == 10000000) {
                                    MessageBox.Show("Running on Thread #" + Thread.CurrentThread.Name);
                                    Die("Error interno del script. on thread #" + Thread.CurrentThread.Name);
                                }
                            }
                            MessageBox.Show("Exit loop");
                            syncctx.Post(ui => { next(); }, null);
                        }
                        catch (Exception ex) {
                            syncctx.Post(ui => { exHandler(ex); }, null);
                        }
                    });
                    t.Name = "Foo";
                    t.Start();
                };

                dlg.Cancel = delegate {
                    MessageBox.Show("Cancel");
                };

                dlg.Show();
            };

            Controls.Add(btnRun);
            btnRunErrOnMain.Top = btnRun.Top + 30;
            Controls.Add(btnRunErrOnMain);
        }
Ejemplo n.º 2
0
        private static void StartIpcPipe()
        {
            //todo: impl a set of IPC APIs to perform some cmds. eg.Pause/Resume, Quit...
            //todo: Temporay IPC mechanism.
            var synCtx = new WindowsFormsSynchronizationContext();//note: won't work with `SynchronizationContext.Current`
            var pipeThread = new Thread(() =>
            {
                while (true)
                {
                    using (var server = new System.IO.Pipes.NamedPipeServerStream("WGestures_IPC_API"))
                    {
                        server.WaitForConnection();

                        Debug.WriteLine("Clien Connected");
                        using (var reader = new StreamReader(server))
                        {
                            var cmd = reader.ReadLine();
                            Debug.WriteLine("Pipe CMD=" + cmd);

                            if (cmd == "ShowSettings")
                            {
                                synCtx.Post((s) =>
                                {
                                    Debug.WriteLine("Thread=" + Thread.CurrentThread.ManagedThreadId);
                                    ShowSettings();
                                }, null);
                            }
                        }
                    }
                }
            }) { IsBackground = true };
            pipeThread.Start();
        }
Ejemplo n.º 3
0
		public FrmMain() {
			this.InitializeComponent();


            Closed += (s, e) => { Environment.Exit(0); };


			Thread.CurrentThread.Name = "UIThread";
			base.IsMdiContainer = false;

			var btnNewFrmST = new Button {
				Text = "New Form (Main Thread)",
				Width = 400
			};

			var prevBtn = btnNewFrmST;
			var btnNewFrmMT = new Button {
				Text = "New form does work on its own Thread (The app remains responsive).",
				Top = prevBtn.Top + prevBtn.Height + 10,
				Width = prevBtn.Width
			};

			prevBtn = btnNewFrmMT;
			var btnNewFrmTsk = new Button {
				Text = "New Form (Task)",
				Top = prevBtn.Top + prevBtn.Height + 10,
				Width = prevBtn.Width
			};

			prevBtn = btnNewFrmTsk;
			var btnNewFrmWork = new Button {
				Text = "New form does work on the UI Thread (Hangs the app).",
				Top = prevBtn.Top + prevBtn.Height + 10,
				Width = prevBtn.Width
			};

			prevBtn = btnNewFrmWork;
			var btnNewFrmWorkAsync = new Button {
				Text = "New Form (Does Async work...)",
				Top = prevBtn.Top + prevBtn.Height + 10,
				Width = prevBtn.Width
			};

			// Controls.Add(btnNewFrmST);
			Controls.Add(btnNewFrmMT);
			// Controls.Add(btnNewFrmTsk);
			Controls.Add(btnNewFrmWork);
			// Controls.Add(btnNewFrmWorkAsync);

			var syncctx = new WindowsFormsSynchronizationContext();

			btnNewFrmMT.Click += (sender, args) => {
				Thread t = new Thread(()=> {

                        while(true) {
                            // Shows a new form
                            syncctx.Post(ui => {
                                    // MessageBox.Show("SyncCtx running on thread #" + Thread.CurrentThread.Name);
                                    var frm = new FrmEx();
                                    try {
                                        frm.Show();
                                    }
                                    catch (Exception ex) {
                                        MessageBox.Show($"Catched inside thread ctx.\n{ex.Message}");
                                    }
                                }, 
                                null);

                            // Simulates some work.
                            for(int i=0; i < 10*1000; i++){
                                WriteLine($"Doing work on #{Thread.CurrentThread.Name} => {i}");

                                Thread.Sleep(0);
                            }

                            Application.DoEvents();

                        }
				});

                t.Name = $"#{new Random().Next()}";
				t.Start();
			};

			btnNewFrmST.Click += (sender, args) => {
				var frm = new FrmEx();
				frm.Show();
			};

			btnNewFrmTsk.Click += (sender, args) => {
				Task<int> tsk = new Task<int>(()=> {
					int result;
					try {
						MessageBox.Show("tsk");
						var frm = new FrmEx();
						frm.ShowDialog();
						result = 0;
					}
					catch {
						MessageBox.Show("catched");
						result = 1;
					}
					return result;
				});

				Action<Task<int>> onErr = delegate(Task<int> t) {
					MessageBox.Show(t.Exception.Message);
				};

				tsk.ContinueWith(onErr, TaskContinuationOptions.OnlyOnFaulted);
				tsk.Start();
			};

			btnNewFrmWork.Click += (sender, args) => {
				var frm = new FrmEx();
				frm.Show();
				int count = 0;
				while (true) {
					count++;
					if (count == 10000000) {
						Thread.Sleep(1000);
						count = 0;
					}
                    WriteLine($"Doing work on #{Thread.CurrentThread.Name} => {count}");
				}
			};

			btnNewFrmWorkAsync.Click += (sender, args) => {
				var frm = new FrmEx();
				frm.Show();
				int count = 0;
				Thread t = new Thread(() => {
					while (true) {
						count++;
						if (count == 10000000) {
							Thread.Sleep(1000);
							count = 0;
						}
					}
				});

				t.Start();
			};
		}