-
Notifications
You must be signed in to change notification settings - Fork 7
/
Timer.linq
44 lines (38 loc) · 987 Bytes
/
Timer.linq
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<Query Kind="Program" />
void Main()
{
//This timer will run forever, but the thread will be kept in front
var timer = new System.Threading.Timer((e) => Create(), null, TimeSpan.Zero, TimeSpan.FromSeconds(2));
Thread.Sleep(Timeout.Infinite);
}
// Main-Thread scenario
// This is all called from main.
public static class Sample
{
public static DateTime timeout { get; set; } = DateTime.UtcNow;
}
public void Create()
{
Sample.timeout = DateTime.UtcNow;
Sample.timeout.Dump();
}
// Non-Main-Thread
// This is all called outside the main thread, but below the Linqpad class
static Timer timer = new Timer(GetPoco, null, 0, Timeout.Infinite);
static Poco poco = new Poco();
public static void GetPoco(object state)
{
Thread.Sleep(1000);
lock (poco)
{
poco = new Poco();
}
poco.Dump();
timer.Change(1000, Timeout.Infinite);
}
public class Poco
{
public int Id { get; set; }
public string Value { get; set; }
public DateTime CurrentTime { get; set; } = DateTime.Now;
}