-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathForm1.cs
More file actions
150 lines (145 loc) · 5.79 KB
/
Form1.cs
File metadata and controls
150 lines (145 loc) · 5.79 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
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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Threading;
namespace WindowsApplication17
{
public partial class Form1 : Form
{
private SqlConnection connection = null;
private SqlCommand command = null;
// The Service Name is required to correctly
// register for notification.
// The Service Name must be already defined with
// Service Broker for the database you are querying.
private const string ServiceName = "ChangeNotifications";
// Spercify how long the notification request
// should wait before timing out.
// This value waits for 60 minutes.
private int NotificationTimeout = 3600;
public Form1()
{
InitializeComponent();
this.WindowState = FormWindowState.Minimized;
this.ShowInTaskbar = false;
notifyIcon1.Visible = false;
}
private string GetConnectionString()
{
// To avoid storing the connection string in your code,
// you can retrive it from a configuration file.
// In general, client applications don't need to incur the
// overhead of connection pooling.
//return "Data Source=localhost;Integrated Security=SSPI;" +
//"Initial Catalog=SQLNotificationRequestDB;Pooling=False;";
return "Data Source=mssql1.gear.host;Initial Catalog=sqlrequestdb;User ID=sqlrequestdb;Password=Fe7Tx--B3ti5";
}
private string GetSQL()
{
return "SELECT Name,Number from SQLNotificationRequestTable";
}
private string GetListenerSQL()
{
// Note that ChangeMessages is the name
// of the Service Broker queue that must
// be already defined.
return "WAITFOR ( RECEIVE * FROM ChangeMessages);";
}
private void StartListener()
{
// A seperate listener thread is needed to
// monitor the queue for notifications.
Thread listener = new Thread(Listen);
listener.Name = "Query Notification Watcher";
listener.Start();
}
private void Listen()
{
using (SqlConnection connection =
new SqlConnection(GetConnectionString()))
{
using (SqlCommand command =
new SqlCommand(GetListenerSQL(), connection))
{
connection.Open();
// Make sure we don't time out before the
// notification request times out.
command.CommandTimeout = NotificationTimeout + 15;
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
messageText = System.Text.ASCIIEncoding.ASCII.GetString((byte[])reader.GetValue(13)).ToString();
// Empty queue of messages.
// Application logic could parse
// the queue data and
// change its notification logic.
}
object[] args = { this, EventArgs.Empty };
EventHandler notify =
new EventHandler(OnNotificationComplete);
// Notify the UI thread that a notification
// has occurred.
this.BeginInvoke(notify, args);
}
}
}
private string messageText;
private void OnNotificationComplete(object sender, EventArgs e)
{
messageText = messageText.Replace("??", "");
label1.Text = messageText.Replace("\0","");
notifyIcon1.Visible = true;
//notifyIcon1.Icon =
notifyIcon1.ShowBalloonTip(1000, "Important Notice", messageText.Replace("\0", "") , ToolTipIcon.Info);
// The user can decide to register
// and request a new notification by
// checking the CheckBox on the form.
GetData();
}
private void GetData()
{
// Make sure the command object does not already have
// a notification object associated with it.
command.Notification = null;
SqlNotificationRequest snr =
new SqlNotificationRequest();
snr.UserData = new Guid().ToString();
snr.Options = "Service=" + ServiceName;
// If a time-out occurs, a notification
// will indicate that is the
// reason for the notification.
snr.Timeout = NotificationTimeout;
command.Notification = snr;
// Start the background listener.
StartListener();
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (connection != null)
{
connection.Close();
}
}
private void Form1_Load(object sender, EventArgs e)
{
if (connection == null)
{
connection = new SqlConnection(GetConnectionString());
}
if (command == null)
{
// GetSQL is a local procedure SQL string.
// You might want to use a stored procedure
// in your application.
command = new SqlCommand(GetSQL(), connection);
}
GetData();
}
}
}