I use the invoke action method in the form works perfectly)

 public void Active(Panel l) { Task.Factory.StartNew(() => { Thread.Sleep(2000); Invoke((Action)(() => { l.Visible = false; })); }); } 

But I don’t know how to work through the class.

I need to call it through the class in the main form (I use buttons)

PS: When a button is pressed, through the class (where the method is called, something is being done) to notify the user that the method worked normally. I pass the information in the label on the form.

[Update]

On each button I call this method:

 Active(panel1); 

so that the panel after 2 seconds was hidden.

I want the Active method to be transferred to the class, and from there I already assign each button a method

  • What about events? - MihailPw
  • @ AGS17, I know little unfortunately, I almost did not work with delegates. - GooliveR
  • You can do without delegates. Use EventHandler - MihailPw
  • @ AGS17 and EventHandler is no longer a delegate? Same delegate, but with a predefined signature and that's it. - rdorn
  • Look at the answer to your question about network monitoring, and replace EventHandler with EventHandler <T> in it. You define your own successor class from EventArgs with the required fields and work with the event, just as you work with them in forms. - rdorn

2 answers 2

What you want can be done like this:

The class in which we call the method

 class ActiveClass { public void Active(Control control) { Task.Factory.StartNew(() => { Thread.Sleep(2000); control.Invoke((Action)(() => control.Hide())); }); } } 

Form Code

 public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { ActiveClass active = new ActiveClass(); active.Active(button1); } } 
  • I want when I clicked on a button, a method was called (which is written in another class) that will hide the panel window on the form after a period of time. Invoke((Action)(() => { l.Visible = false; })); in void Active(Panel l) {...{ l.Visible = false; } } so that other panels can be set to void Active(Panel l) {...{ l.Visible = false; } } void Active(Panel l) {...{ l.Visible = false; } } - GooliveR
  • Updated) can now clearly be what I want) - GooliveR
  • @ArteS, I redid the answer - Umed

I found a solution to this problem) As I wanted through the class!

 public static void Active(Panel l) { Task.Factory.StartNew(() => { Thread.Sleep(2000); l.Invoke((Action)(() => { l.Visible = false; })); }); }