static void Main(string[] args) { var mre = new ManualResetEventSlim(); var memoryTask = Task.Run(() => { while (!mre.IsSet) { Console.SetCursorPosition(0, 0); Console.WriteLine($"Process ID: {Environment.ProcessId}"); Console.Write("Working, press any key to stop..."); Console.WriteLine(); GCUtils.PrintInfoAboutLastCycle(); Thread.Sleep(1000); } }); var task = Task.Run(async() => { //this cts is simulation of a service-wide one that will fire when the service needs to shut down var systemCts = new CancellationTokenSource(); var mockHttp = new MockHttpMessageHandler(); mockHttp.When("/") .Respond(HttpStatusCode.OK); using (var httpClient = mockHttp.ToHttpClient()) //simulate "static" http client { while (!mre.IsSet) { //simulate API linking cancellation token source with main one that indicates system lifetime var localCts = CancellationTokenSource.CreateLinkedTokenSource(systemCts.Token); localCts.CancelAfter(2000); using (var content = new StringContent(JsonSerializer.Serialize(new { Foo = "Bar" }))) using (HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, new Uri("http://foobar.non-existing", UriKind.RelativeOrAbsolute))) { request.Content = content; using (var result = await httpClient.SendAsync(request, localCts.Token)) { //do stuff with the result } } } } }); Console.ReadKey(); mre.Set(); Task.WaitAll(task, memoryTask); Console.WriteLine("OK, bye!"); }
static void Main(string[] args) { var refs = new List <object>(); var mre = new ManualResetEventSlim(); var i = 0; var memoryTask = Task.Run(() => { while (!mre.IsSet) { Console.SetCursorPosition(0, 0); Console.WriteLine($"Process ID: {Environment.ProcessId}"); Console.Write("Working, press any key to stop..."); Console.WriteLine(); GCUtils.PrintInfoAboutLastCycle(); Thread.Sleep(1000); } }); var task = Task.Run(() => { while (!mre.IsSet) { if (i % 5 == 0) { new ApplicationObjectB(); } if (i % 8 == 0) { new ApplicationObjectA(); } if (i % 10 == 0) { new ApplicationObjectC(); } if (i % 50 == 0) { refs.Add(new ApplicationObjectD()); } i++; } }); Console.ReadKey(); mre.Set(); Task.WaitAll(task, memoryTask); }
static void Main(string[] args) { var mre = new ManualResetEventSlim(); var memoryTask = Task.Run(() => { while (!mre.IsSet) { Console.SetCursorPosition(0, 0); Console.WriteLine($"Process ID: {Environment.ProcessId}"); Console.Write("Working, press any key to stop..."); Console.WriteLine(); GCUtils.PrintInfoAboutLastCycle(); Thread.Sleep(1000); } }); ThreadPool.SetMinThreads(1000, 100); var task = Task.Run(() => { var tl = new CloseableThreadLocal(); int x = 0; while (!mre.IsSet) { Task.Run(() => { tl.Set("hello!"); _ = tl.Get(); Thread.Sleep(10); }); if (++x % 1000 == 0) { GC.Collect(2); GC.WaitForPendingFinalizers(); } } }); Console.ReadKey(); mre.Set(); Task.WaitAll(task, memoryTask); Console.WriteLine("OK, bye!"); }
static void Main(string[] args) { var mre = new ManualResetEventSlim(); var memoryTask = Task.Run(() => { while (!mre.IsSet) { Console.SetCursorPosition(0, 0); Console.WriteLine($"Process ID: {Environment.ProcessId}"); Console.Write("Working, press any key to stop..."); Console.WriteLine(); GCUtils.PrintInfoAboutLastCycle(); Thread.Sleep(1000); } }); var task = Task.Run(() => { var timer = new System.Timers.Timer(500); timer.Start(); int x = 0; while (!mre.IsSet) { var eventDispatcher = new TimerEventDispatcher(timer); eventDispatcher.TimerElapsedNow += EventDispatcher_TimerElapsedNow; if (++x % 2000 == 0) { Thread.Sleep(50); } } }); Console.ReadKey(); mre.Set(); Task.WaitAll(task, memoryTask); Console.WriteLine("OK, bye!"); }
static void Main(string[] args) { var mre = new ManualResetEventSlim(); var memoryTask = Task.Run(() => { while (!mre.IsSet) { Console.SetCursorPosition(0, 0); Console.WriteLine($"Process ID: {Environment.ProcessId}"); Console.Write("Working, press any key to stop..."); Console.WriteLine(); GCUtils.PrintInfoAboutLastCycle(); Thread.Sleep(1000); } }); var task = Task.Run(() => { while (!mre.IsSet) { //note: PhysicalFileProvider under the hood uses unmanaged resources (see https://github.com/dotnet/runtime/blob/master/src/libraries/System.IO.FileSystem.Watcher/src/System/IO/FileSystemWatcher.Win32.cs#L32) try { var fp = new PhysicalFileProvider(Path.GetTempPath()); fp.Watch("*.*"); } catch (Exception) { } } }); Console.ReadKey(); mre.Set(); Task.WaitAll(task, memoryTask); Console.WriteLine("OK, bye!"); }