process from console compared to winform
c#, delegates, winforms
Solution
The `Console` class was intentionally written to be thread safe. You can call it from any thread. It will even make sure to not to 'overlap' calls from different threads. `Console.Write/WriteLine` is atomic. This is because Console is designed to interact with a shell, and one major purpose of a shell is to be able to gather input from multiple processes. It's a decent amount of work to have it be able to do that, but it needed to be done for it to realistically serve its purpose.
GUI objects, such as labels, were not designed with that in mind.
Problem
The following console application runs ok - I was surprised it did not error. ``` class DelegateExperiments { //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> //open notepad when the console begins //create an event that fires and writes "Notepad closed" in the console //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> //instance variables of the form private const string helloText = "hello world"; private const string exitText = "you just closed notepad"; private Process myProcess; //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< static void Main(string[] args) { Console.WriteLine(helloText); DelegateExperiments myInstance; myInstance = new DelegateExperiments(); myInstance.Foo(); Console.Read(); } void Foo() { myProcess = new Process(); myProcess.StartInfo.FileName = @"notepad.exe"; myProcess.Exited += MyProcessExited; myProcess.EnableRaisingEvents = true; //myProcess.SynchronizingObject = this; myProcess.Start(); } private void MyProcessExited(Object source, EventArgs e) { Console.WriteLine(exitText); } } ``` If I try to do something similar with a winform i.e write a message back to a label on the form then it's more complicated and needs the line `myProcess.SynchronizingObject = this;` to work. Why should they be different?