Передача в поток метода с параметрами - C#
Формулировка задачи:
Нужно запустить метод ScreenUpdate(Pet)
Без проблем могу вызвать метод без использования потока (thread)
ScreenUpdate(Pet);
Но с созданием потока - проблемы (скрин1)
Листинг программы
- static void ScreenUpdate(Property Pet)
- {
- while (true)
- {
- Console.SetCursorPosition(0, 0);
- Console.WriteLine(Pet.Name);
- Console.SetCursorPosition(0, 1);
- Console.WriteLine("Health " + Pet.Health);
- Console.SetCursorPosition(0, 2);
- Console.WriteLine("Starveness " + Pet.Starveness);
- Console.SetCursorPosition(0, 3);
- Console.WriteLine("Happiness " + Pet.Happiness);
- Console.SetCursorPosition(0, 4);
- Console.WriteLine("Toilet " + Pet.Toilet);
- Console.SetCursorPosition(0, 5);
- Thread.Sleep(1000);
- }
- }
Листинг программы
- Thread Screen = new Thread(new ParameterizedThreadStart(ScreenUpdate(Pet)));
- Screen.IsBackground = true;
- Screen.Start();
Решение задачи: «Передача в поток метода с параметрами»
textual
Листинг программы
- Thread Screen = new Thread(ScreenUpdate);
- Screen.IsBackground = true;
- Screen.Start(Pet);
- static void ScreenUpdate(object param)
- {
- Property Pet = (Property)param;
- while (true)
- {
- Console.SetCursorPosition(0, 0);
- Console.WriteLine(Pet.Name);
- Console.SetCursorPosition(0, 1);
- Console.WriteLine("Health " + Pet.Health);
- Console.SetCursorPosition(0, 2);
- Console.WriteLine("Starveness " + Pet.Starveness);
- Console.SetCursorPosition(0, 3);
- Console.WriteLine("Happiness " + Pet.Happiness);
- Console.SetCursorPosition(0, 4);
- Console.WriteLine("Toilet " + Pet.Toilet);
- Console.SetCursorPosition(0, 5);
- Thread.Sleep(1000);
- }
- }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д