-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathTimerExample.cs
More file actions
96 lines (77 loc) · 2.63 KB
/
TimerExample.cs
File metadata and controls
96 lines (77 loc) · 2.63 KB
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
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Diagnostics;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
using Microsoft.ServiceBus.Messaging;
namespace WindowsService1
{
class TimerExample
{
private Timer _Timer = null;
public void Start()
{
_Timer = new Timer(10000); //30 seconds
_Timer.Elapsed += async (sender, e) => await HandleTimer();
_Timer.AutoReset = true;
_Timer.Enabled = true;
Console.WriteLine("TimerExample Timer started");
}
private static async Task<bool> HandleTimer()
{
var op = StackifyLib.ProfileTracer.CreateAsOperation("Timer.DownloadPage");
var result = await op.ExecAsync(async () =>
{
try
{
Debug.WriteLine("TimerExample: Downloading page");
using (HttpClient client = new HttpClient())
{
var data = await client.GetAsync("https://www.microsoft.com/en-us/");
var content = await data.Content.ReadAsStringAsync();
Console.WriteLine("Finished downloading Microsoft page. Length:" + content.Length);
}
return true;
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
return false;
});
var op2 = StackifyLib.ProfileTracer.CreateAsOperation("Timer.WriteToQueue");
var result2= await op2.ExecAsync(async () =>
{
try
{
Debug.WriteLine("TimerExample: Writing to queue");
var client = QueueClient.CreateFromConnectionString(ConfigurationManager.AppSettings["Microsoft.ServiceBus.ConnectionString"], "WindowsService1");
var message = new BrokeredMessage("This is a test message!");
client.Send(message);
return true;
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
return false;
});
return result;
}
public void Stop()
{
try
{
_Timer.Stop();
_Timer.Dispose();
}
catch (Exception)
{
}
}
}
}