Task.Delay
Method
Creates a task that will
complete after a time delay.
The time span to wait before
completing the returned task, or TimeSpan.FromMilliseconds(-1) to wait indefinitely.
(for an unlimited or unspecified period of time)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace AlarmClock
{
public partial class Form4 : Form
{
public Form4()
{
InitializeComponent();
}
private void Form4_Load(object sender, EventArgs e)
{
timer1.Interval = 1000;
timer1.Enabled = true;
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
// Everytime a second this timer method is invoke, the "new thread" is created.
for (int i = 0; i < 8; i++)
{
Console.WriteLine(i + "- Start " +
DateTime.Now.ToString("MM-dd-yyyy
HH:mm:ss.fff") + " " +
Thread.CurrentThread.ManagedThreadId);
Task.Delay(1000).ContinueWith((x) => SomeMethod(i));
Console.WriteLine(i + "- Stop " +
DateTime.Now.ToString("MM-dd-yyyy
HH:mm:ss.fff") + " " +
Thread.CurrentThread.ManagedThreadId);
}
}
static void SomeMethod(int i )
{
Console.WriteLine(i + "- Process
" +
DateTime.Now.ToString("MM-dd-yyyy
HH:mm:ss.fff") + " --- " + " " +
Thread.CurrentThread.ManagedThreadId);
}
}
}
Output
0 Start 12-21-2019 21:20:15.313 1
0 Stop 12-21-2019 21:20:15.320 1
1 Start 12-21-2019 21:20:15.320 1
1 Stop 12-21-2019 21:20:15.320 1
2 Start 12-21-2019 21:20:15.320 1
2 Stop 12-21-2019 21:20:15.320 1
3 Start 12-21-2019 21:20:15.320 1
3 Stop 12-21-2019 21:20:15.320 1
4 Start 12-21-2019 21:20:15.320 1
4 Stop 12-21-2019 21:20:15.320 1
5 Start 12-21-2019 21:20:15.320 1
5 Stop 12-21-2019 21:20:15.320 1
6 Start 12-21-2019 21:20:15.320 1
6 Stop 12-21-2019 21:20:15.320 1
7 Start 12-21-2019 21:20:15.320 1
7 Stop 12-21-2019 21:20:15.320 1
0 Start 12-21-2019 21:20:16.327 1
0 Stop 12-21-2019 21:20:16.327 1
1 Start 12-21-2019 21:20:16.327 1
1 Stop 12-21-2019 21:20:16.327 1
2 Start 12-21-2019 21:20:16.327 1
2 Stop 12-21-2019 21:20:16.327 1
3 Start 12-21-2019 21:20:16.327 1
3 Stop 12-21-2019 21:20:16.327 1
4 Start 12-21-2019 21:20:16.327 1
4 Stop 12-21-2019 21:20:16.327 1
5 Start 12-21-2019 21:20:16.327 1
5 Stop 12-21-2019 21:20:16.327 1
6 Start 12-21-2019 21:20:16.327 1
6 Stop 12-21-2019 21:20:16.327 1
7 Start 12-21-2019 21:20:16.327 1
7 Stop 12-21-2019 21:20:16.327 1
8 Process 12-21-2019 21:20:16.358 --- 5
8 Process 12-21-2019 21:20:16.358 --- 5
8 Process 12-21-2019 21:20:16.358 --- 5
8 Process 12-21-2019 21:20:16.358 --- 5
8 Process 12-21-2019 21:20:16.358 --- 5
8 Process 12-21-2019 21:20:16.358 --- 4
8 Process 12-21-2019 21:20:16.358 --- 6
8 Process 12-21-2019 21:20:16.358 --- 7
0 Start 12-21-2019 21:20:17.340 1
0 Stop 12-21-2019 21:20:17.340 1
1 Start 12-21-2019 21:20:17.340 1
1 Stop 12-21-2019 21:20:17.340 1
2 Start 12-21-2019 21:20:17.340 1
2 Stop 12-21-2019 21:20:17.340 1
3 Start 12-21-2019 21:20:17.340 1
3 Stop 12-21-2019 21:20:17.340 1
4 Start 12-21-2019 21:20:17.340 1
4 Stop 12-21-2019 21:20:17.340 1
5 Start 12-21-2019 21:20:17.340 1
5 Stop 12-21-2019 21:20:17.340 1
6 Start 12-21-2019 21:20:17.340 1
6 Stop 12-21-2019 21:20:17.340 1
7 Start 12-21-2019 21:20:17.340 1
7 Stop 12-21-2019 21:20:17.340 1
8 Process 12-21-2019 21:20:17.340 --- 5
8 Process 12-21-2019 21:20:17.340 --- 5
8 Process 12-21-2019 21:20:17.340 --- 5
8 Process 12-21-2019 21:20:17.340 --- 5
8 Process 12-21-2019 21:20:17.340 --- 5
8 Process 12-21-2019 21:20:17.340 --- 5
8 Process 12-21-2019 21:20:17.340 --- 5
8 Process 12-21-2019 21:20:17.340 --- 5
8 Process 12-21-2019 21:20:18.354 --- 5
8 Process 12-21-2019 21:20:18.354 --- 5
8 Process 12-21-2019 21:20:18.354 --- 5
8 Process 12-21-2019 21:20:18.354 --- 5
8 Process 12-21-2019 21:20:18.354 --- 5
8 Process 12-21-2019 21:20:18.354 --- 5
8 Process 12-21-2019 21:20:18.354 --- 5
8 Process 12-21-2019 21:20:18.354 --- 5
0 Start 12-21-2019 21:20:18.357 1
0 Stop 12-21-2019 21:20:18.357 1
1 Start 12-21-2019 21:20:18.357 1
1 Stop 12-21-2019 21:20:18.357 1
2 Start 12-21-2019 21:20:18.357 1
2 Stop 12-21-2019 21:20:18.357 1
3 Start 12-21-2019 21:20:18.357 1
3 Stop 12-21-2019 21:20:18.357 1
4 Start 12-21-2019 21:20:18.357 1
4 Stop 12-21-2019 21:20:18.357 1
5 Start 12-21-2019 21:20:18.357 1
5 Stop 12-21-2019 21:20:18.358 1
6 Start 12-21-2019 21:20:18.358 1
6 Stop 12-21-2019 21:20:18.358 1
7 Start 12-21-2019 21:20:18.358 1
7 Stop 12-21-2019 21:20:18.358 1
No comments:
Post a Comment