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.

  • one
    It's not a matter of business ... - Qwertiy
  • Thread.Sleep in UI can not be used, this is one of the biggest differences. - VladD
  • The 2006 book is a very old book. Already 10 years have passed, during this time in C # a lot of things have appeared. Find a newer version. - Bulson

1 answer 1

The label1 displays the wrong text, because its Text property is assigned the result of calling the ToString method on an object that is a delegate of the method. But in reality, the result of the delegate’s execution should be placed in a text variable: label1.Text = method();

For this, it is necessary to change both the delegate itself and the implementation of the corresponding method.

 public delegate string TopMessage(int seconds); static string Show (int seconds) { return string.Format("How long before closing the console: " + seconds); } 

In order not to freeze the UI while waiting for one second, use the Timer component instead of the ThreadSleep method.