public void GetWeather(object obj) { WeatherQuery w = (WeatherQuery)obj; int n = rand.Next(0, 100); int c = rand.Next(0, 5); w.cntrs = (Countries)Enum.GetValues(typeof(Countries)).GetValue(c); w.Country = Enum.GetValues(typeof(Countries)).GetValue(c).ToString(); if (n <= 33) { Console.WriteLine($"В стране {w.Country} в {DateTime.Now.ToLongTimeString()} солнечно."); } else if (n > 66) { Console.WriteLine($"В стране {w.Country} в {DateTime.Now.ToLongTimeString()} выпадают осадки."); } else { Console.WriteLine($"В стране {w.Country} в {DateTime.Now.ToLongTimeString()} облачно."); } }
static void Main(string[] args) { //#1 var allProcesses = Process.GetProcesses(); foreach (var proc in allProcesses) { try { Console.WriteLine($"ID: {proc.Id}\nName: {proc.ProcessName}\nPriority: {proc.PriorityClass}\nLaunch Time: {proc.StartTime}\nProcessor Time: {proc.TotalProcessorTime}"); } catch (Exception e) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine(e.Message); Console.ResetColor(); } Console.WriteLine(); } //#2 { Console.WriteLine("\n### Работа с доменами ###\n"); AppDomain appDom = AppDomain.CurrentDomain; Console.WriteLine($"Current Domain's Name: {appDom.FriendlyName}\nId: {appDom.Id}"); Console.WriteLine("\nDomain loaded assemblies:"); Assembly[] assembs = appDom.GetAssemblies(); foreach (var assemb in assembs) { Console.WriteLine(assemb.ToString()); } AppDomain newDomain = AppDomain.CreateDomain("MyNewDomain"); newDomain.Load("SayHelloLib"); Assembly[] loadedDomainAssemb = newDomain.GetAssemblies(); Console.WriteLine("\nЗагруженные сборки созданного домена:"); foreach (var assemb in loadedDomainAssemb) { Console.WriteLine(assemb.ToString()); } AppDomain.Unload(newDomain); } //#3 { Console.WriteLine("\n### Работа с отдельным потоком ###\n"); Thread th = new Thread(CountPrime); th.Name = "Поток вывода простых чисел на консоль"; StreamWriter Writer = new StreamWriter("Primes.txt"); CountPrimes2 cprimes = new CountPrimes2(Writer, 200); Thread th2 = new Thread(cprimes.Count); th2.Name = "Поток записи простых чисел в файл"; th2.Start(); th.Start(200); Thread.Sleep(2000); th.Suspend(); Console.Write("Pausing second thread for a 3 secs "); Console.WriteLine(DateTime.Now); Console.WriteLine("Thread status: " + th.ThreadState); Console.WriteLine("Thread name: " + th.Name); Console.WriteLine("Thread priority: " + th.Priority); Console.WriteLine("Thread ID: " + th.ManagedThreadId); th2.Suspend(); Writer.Write("Paused input for 3 seconds "); Writer.WriteLine(DateTime.Now); Writer.WriteLine("Thread status: " + th2.ThreadState); Writer.WriteLine("Thread name: " + th2.Name); Writer.WriteLine("Thread priority: " + th2.Priority); Writer.WriteLine("Thread ID: " + th2.ManagedThreadId); Thread.Sleep(3000); Console.Write("Continue processing... "); Console.WriteLine(DateTime.Now); th.Resume(); Console.WriteLine("Thread status: " + th.ThreadState); Writer.Write("Continued input..."); Writer.WriteLine(DateTime.Now); th2.Resume(); Writer.WriteLine("Thread status: " + th2.ThreadState); } //#4 Console.WriteLine("\n### Работа с двумя потоками ###\n"); Thread ThEven = new Thread(PrintEven); Thread ThOdd = new Thread(PrintOdd) { Priority = ThreadPriority.Lowest }; Console.WriteLine("Различные приоритеты потоков: "); ThEven.Start(10000); ThOdd.Start(10000); while (ThEven.IsAlive || ThOdd.IsAlive) { } Console.WriteLine(); ThWhite.Start(1198); ThRed.Start(598); while (ThWhite.IsAlive) { } Console.WriteLine(); Console.WriteLine("\nСначала четные, потом нечетные."); Thread ThEven2 = new Thread(PrintEven2); Thread ThOdd2 = new Thread(PrintOdd2); ThEven2.Start(200); Thread.Sleep(10); ThOdd2.Start(200); while (ThOdd2.IsAlive) { } Writer2.Close(); Console.WriteLine("\nОдно четное, одно нечетное."); Thread ThEven3 = new Thread(PrintEven3); Thread ThOdd3 = new Thread(PrintOdd3); ThEven3.Start(100); ThOdd3.Start(100); while (ThOdd3.IsAlive) { } Console.WriteLine(); Writer3.Close(); Console.WriteLine("\nРабота с классом Timer."); WeatherQuery query = new WeatherQuery("Belarus", new DateTime(), rand); TimerCallback timerCallbackDelegate = query.GetWeather; //Timer weatherQueryTimer = new Timer(timerCallbackDelegate, query, 1000, 2000); Console.ReadLine(); }