Good day. Read the book by Paul Deytel - The Complete Guide to C # 2006. I got to the section with delegates. After reading the chapter, it seemed to me for a moment that I understood the topic, because I was able to implement a simple code that counts to 10:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; namespace consoledemo { public delegate void TopMessage(string message); //объявление делегата. Делегат - это тип данных по ссылке. class Program { static void Main(string[] args) { TopMessage method = Show; // Тут объявлена ссылка на метод с совпадающей сигнатурой (Show) ShowMessage(10, method); //method передаётся сюда вторым параметром } static void ShowMessage(int second, TopMessage method)//TopMessage передаёт ссылку на метод совпадающей сигнатурой. method является переменной, в которой хранится ссылка { for (int i = 0; i < second; i++) { Thread.Sleep(1000); method(string.Format("How long before closing the console: " + i)); //method транслирует свои сообщения в другой метод } } static void Show (string message) // данный метод имеет сигнатуру точно такую же, как объявлено в делегате (TopMessage) { Console.WriteLine(message); } } } But since Deitel in his book demonstrates most of the examples in the console, I decided to complicate my task a little. I add a button and a label. By pressing the button, the message How long before closing the console: should have been translated into the label How long before closing the console: after that, the label should have become invisible. If the problem with the disappearance of the label is completely solved, then the implementation of the display of the message is a problem for me. If I add to the Main method: label1.Text = method.ToString(); I’ll get the window freeze for 10 seconds (since I didn’t bring this business to a separate thread), and upon completion, the label will display the text Win32Demo.TopMessage . The maximum that I could do is connect the MessageBox.Show class.
How do I get the Show method to translate my messages into the properties Button1.text , Label1.text and so on.
Thanks for the replies, Amateur.
Thread.Sleepin UI can not be used, this is one of the biggest differences. - VladD