Home > Net >  C # how to create a specified task execution time
C # how to create a specified task execution time

Time:09-26

The situation is about

Set a task - & gt; Let him to perform an Open () method

And then 10 seconds after it performs a Close () method

CodePudding user response:

And ask why baidu to the example they are to create static Task
The static Task no horse directly

CodePudding user response:

 public partial class Form1: Form 
{
Public _click ()
{
InitializeComponent ();
Non-static A=new non-static ();
A. method (1);
}

}
The class non-static
{
Public void method (1)
{
New Thread (the delegate ()
{
//you to execute method
}). The Start ();
}
}

So I can

CodePudding user response:

Do not need new static class, life period of the program is run (singleton), there is only one instance can be used directly,
Non-static class need generated after the new instance (object) to use,
Task not provided must use a static class, class is generally used like this:
Task t1=new Task (Open);//the Open is you the Open method
T1. Start ();

//wait for 10 seconds
System. Threading. Thread. Sleep (10 * 1000);

Task t2=new Task (Close);//Close is your Close method
T2. Start ();

CodePudding user response:

To search for "TimerCallback"
  •  Tags:  
  • C#
  • Related