-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhow-to-implement-timer-netcoreapp1-0-netcoreapp1-1.html
210 lines (195 loc) · 7.87 KB
/
how-to-implement-timer-netcoreapp1-0-netcoreapp1-1.html
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
<!DOCTYPE html>
<html lang="en-US" xml:lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
<title>How to implement a timer with .NETCoreApp1.0 or .NETCoreApp1.1</title>
<link rel="shortcut icon" href="/wwwroot/favicon.ico" />
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<meta name="language" content="en" />
<meta name="keywords" content=".NETCoreApp1.0,.NETCoreApp1.1,NETCoreApp1.0,NETCoreApp1.1,NETCoreApp,net core app" />
<meta name="description" content="How to implement a timer with .NETCoreApp1.0 or .NETCoreApp1.1" />
<meta name="author" content="Adrien Torris" />
<meta name="robots" content="index,follow" />
<meta name="generator" content="SiteUp! 0.1" />
<!--<link rel="alternate" href="https://adrientorris.github.io/aspnet-core/how-to-implement-timer-netcoreapp1-0-netcoreapp1-1.html" hreflang="en-en" />-->
<link href="/wwwroot/css/style.css" rel="stylesheet" />
<script defer src="https://use.fontawesome.com/releases/v5.0.7/js/all.js"></script>
</head>
<body>
<div id="main_ctn" class="post_ctn">
<header>
<div id="header-content">
<h2>Blog</h2>
<ul id="social_links">
<li id="twitter_link">
<a href="https://twitter.com/AdrienTorris" target="_Blank" rel="nofollow" title="Adrien Torris sur Twitter">
<i class="fab fa-twitter fa-2x"></i>
</a>
</li>
<li id="github_link" title="Adrien Torris sur GitHub">
<a href="https://github.com/AdrienTorris" target="_Blank" rel="nofollow">
<i class="fab fa-github fa-2x"></i>
</a>
</li>
<li id="so_link">
<a href="https://stackoverflow.com/users/story/4428633" target="_Blank" rel="nofollow" title="Adrien Torris sur StackOverflow">
<i class="fab fa-stack-overflow fa-2x"></i>
</a>
</li>
<li id="linkedin_link">
<a href="https://www.linkedin.com/in/adrientorris/" target="_Blank" rel="nofollow" title="Adrien Torris sur LinkedIn">
<i class="fab fa-linkedin fa-2x"></i>
</a>
</li>
<li id="gplus_link">
<a href="https://plus.google.com/u/0/+AdrienTorris" target="_Blank" rel="nofollow" title="Adrien Torris sur Google Plus">
<i class="fab fa-google-plus-g fa-2x"></i>
</a>
</li>
</ul>
</div>
</header>
<div id="arianne_wrapper">
<ul>
<li><a href="https://adrientorris.github.io/index.html" title="">Home</a></li>
<li>ASP.NET Core</li>
<li class="active"><a href="https://adrientorris.github.io/aspnet-core/how-to-implement-timer-netcoreapp1-0-netcoreapp1-1.html" title="How to implement a timer with .NETCoreApp1.0 or .NETCoreApp1.1">How to implement a timer with .NETCoreApp1.0 or .NETCoreApp1.1</a></li>
</ul>
</div>
<div id="post_ctn">
<h1>How to implement a timer with .NETCoreApp1.0 or .NETCoreApp1.1</h1>
<p>For a .NET Core project, I needed to implement a timer, to execute some tasks at regular intervals.</p>
<p>Before ASP.NET core, I used to use the System.Timers dll, who provides all the things I needed to implement the timer I wanted to, who was something like that :</p>
<pre><code class="csharp">
public class Class1
{
Timer tm = null;
public Class1()
{
this.tm.Elapsed += new ElapsedEventHandler(Timer_Elapsed);
this.tm.AutoReset = true;
this.tm = new Timer(60000);
}
protected void Timer_Elapsed(object sender, ElapsedEventArgs e)
{
this.tm.Stop();
try
{
// My business logic here
}
catch (Exception)
{
throw;
}
finally
{
this.tm.Start();
}
}
}
</code></pre>
<p>With ASP.NET Core, you can't use System.Timers anymore, you have to use the package System.Threading.Timer, who provides almost the same things.</p>
<div class="pic_wrapper"><img src="/wwwroot/images/dotnet-core/how-to-implement-timer-netcoreapp1-0-netcoreapp1-1.png" alt="How to implement a timer with .NETCoreApp1.0 or .NETCoreApp1.1" title="How to implement a timer with .NETCoreApp1.0 or .NETCoreApp1.1" /></div>
<p>This is a quick example of a timer implemented with this package :</p>
<pre><code class="csharp">
namespace ConsoleApp
{
using System;
using System.Threading;
public class Program
{
Timer _tm = null;
AutoResetEvent _autoEvent = null;
private int _counter = 0;
public static void Main(string[] args)
{
Program p = new Program();
p.StartTimer();
}
public void StartTimer()
{
_autoEvent = new AutoResetEvent(false);
_tm = new Timer(Execute, _autoEvent, 1000, 1000);
Console.Read();
}
public void Execute(Object stateInfo)
{
if (_counter < 10)
{
Console.WriteLine("Call #" + _counter);
_counter++;
return;
}
Console.WriteLine("Final call");
_tm.Dispose();
}
}
}
</code></pre>
<p>If after adding the package System.Threading.Timer in your project, your project doesn't build anymore, it's because you have to add a depency to the Microsoft.NETCore.App platform type to your .NET Core framework :</p>
<pre><code class="json">
{
"version": "1.0.0-*",
"buildOptions": {
"emitEntryPoint": true
},
"dependencies": {
"Microsoft.NETCore.App": "1.1.0" // REMOVE THIS LINE
"System.Threading.Timer": "4.3.0"
},
"frameworks": {
"netcoreapp1.1": {
"dependencies": {
"Microsoft.NETCore.App": {
"type": "platform",
"version": "1.1.0"
}
},
"imports": "dnxcore50"
}
}
}
</code></pre>
<p>Note that if you add the dependency to the netcoreapp framework, you have to remove the first dependency line to the NET Core framework, otherwise you will have the error : <i>An item with the same key has already been added. Key: Microsoft.NETCore.App</i></p>
<div id="crdny">January 16, 2017</div>
<div id="tags_wrapper">
<ul>
<li>ASP.NET Core</li>
<li>.NETCoreApp1.0</li>
<li>.NETCoreApp1.1</li>
<li>Timer</li>
<li>System.Timers</li>
<li>System.Threading.Timer</li>
</ul>
</div>
<div id="refs_wrapper">
<ul>
<li><a href="https://msdn.microsoft.com/en-us/library/system.threading.timer(v=vs.110).aspx" title="System.Threading.Timer documentation" target="_Blank">System.Threading.Timer msdn page</a></li>
</ul>
</div>
</div>
<footer>
</footer>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js" type="text/javascript"></script>
<script src="/wwwroot/js/Infra.js" type="text/javascript"></script>
<link type="text/css" rel="stylesheet" href="/wwwroot/lib/highlight/styles/vs.css" />
<script type="text/javascript" src="/wwwroot/lib/highlight/highlight.pack.js"></script>
<script type="text/javascript">hljs.initHighlightingOnLoad();</script>
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-85948839-1', 'auto');
ga('send', 'pageview');
</script>
<script async src="https://www.googletagmanager.com/gtag/js?id=G-F924P8Y6HC"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-F924P8Y6HC');
</script>
</body>
</html>