Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
6.4 Parallelism: threads and events
//6.4 Parallelism: threads and events using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; using System.Threading; using System.Threading.Tasks; namespace Rextester { public class Program { public static void Main(string[] args) { //It's important to realize that event handlers run on the same thread which triggers them. //That's because triggering the event is merely calling all the methods that subscribed to the event in the order of subscription. var job = new Job(); job.SomeEvent += () => { Console.WriteLine("This code runs on the thread that triggered the event: {0}", Thread.CurrentThread.ManagedThreadId); }; Console.WriteLine("Main thread: {0}", Thread.CurrentThread.ManagedThreadId); var task = Task.Run(() => job.Fire()); task.Wait(); } } public class Job { public delegate void SomeDelegate(); public event SomeDelegate SomeEvent; public void Fire() { Console.WriteLine("I'm a newly started thread: {0}", Thread.CurrentThread.ManagedThreadId); if(SomeEvent != null) { SomeEvent(); //trigger the event handler(s) } } } }
run
|
edit
|
history
|
help
0
makingNum
Sum Matrix Column
https://rextester.com/ZZVYV85848
Find median in a stream
Math Main
jdiwnnx cf
hacktonxx
C# URI parts url
1
Fibonacci Series