[英]Implementing an Event Handler C#
A c#
application is writting on a MS SQL
DB table at several times. 一个
c#
应用程序在MS SQL
DB表上多次编写。 the records of this table must be read and procceessed by another c#
application. 该表的记录必须由另一个
c#
应用程序读取和处理。
At the moment I have implemented I Threading timer which looks (every 2 secs) if the table has rows and proccess the data: 目前,我已经实现了我的线程计时器,该计时器看起来(每2秒)如果表中有行并处理数据:
System.Threading.Timer checkTimer;
checkTimer = new System.Threading.Timer(Callback, null, 2000, 500);
private void InitSQLCon()
{
Con = new SqlConnection(ConectionString);
Con.Open();
}
private void Callback(Object state)
{
string queryString = "Select [Wi_Customer ID],[Wi_License_plate_ID] from " + Properties.Settings.Default.Table1 + " where Wi_Car_Rfid='" + s + "'";
SqlCommand command = new SqlCommand(queryString, Con);
SqlDataReader reader = command.ExecuteReader();
if (reader.HasRows)
{
///...make neccessary operations
}
}
My problem is that the current implementation isn't effecient. 我的问题是当前的实现效率不高。 Checking the table with a timer is resource consuming.
使用计时器检查表会消耗资源。 I would like to do it in an event driven way.
我想以事件驱动的方式来做。 Ideally I would like to implement and event handler raised by the
add record to table1
event. 理想情况下,我想实现由
add record to table1
事件的add record to table1
引发的事件处理程序。 If this possible (since I have never implemented an event handler) I would appreciate any feedbacks on how this can be done. 如果可能的话(因为我从未实现过事件处理程序),我将不胜感激如何完成此操作。
There are some change tracking features in SQL Server, most notable exposed via SqlDependency
- however, frankly I think you'd do better to look at a separate notification mechanism. SQL Server中有一些更改跟踪功能,最值得注意的是通过
SqlDependency
公开的-但是,坦率地说,我认为您最好查看一个单独的通知机制。 For example, I'm a big fan of redis pub/sub, because it is ridiculously simple to set up (heck, a dedicated pub-sub server doesn't even need persistence, so the "bgsave"/"fork" issue that makes redis tricky on windows doesn't apply, so you could just use the redis-server available on nuget). 例如,我是redis pub / sub的忠实拥护者,因为它的设置非常简单(哎呀,专用的pub-sub服务器甚至不需要持久性,因此出现“ bgsave” /“ fork”问题使得redis在Windows上棘手不适用,因此您可以使用nuget上的redis-server。 Then you just have your worker subscribe to a named channel, and you have the other parts of the system broadcast a message to that named channel when they add work.
然后,您只需让您的工作人员订阅一个命名频道,然后系统的其他部分在他们添加工作时就会向该命名频道广播一条消息。 Simple and efficient.
简单高效。 For robustness, you'd also want to periodically poll manually - but probably on a much slower poll - maybe every 30 seconds or something.
为了提高鲁棒性,您还希望定期进行手动轮询-但可能要慢得多-可能每30秒左右一次。
Here's an example of pub/sub using redis via BookSleeve (you will also need a redis-server running on the local machine): 这是通过BookSleeve使用redis的pub / sub的示例(您还将需要在本地计算机上运行的redis服务器 ):
using System;
using System.Text;
using BookSleeve;
static class Program
{
static void Main()
{
// IMPORTANT: the "pub" and "sub" can be on entirely separate machines,
// as long as they are talking to the same server. They are only shown
// together here for convenience
using (var sub = new RedisSubscriberConnection("localhost"))
using (var pub = new RedisConnection("localhost"))
{
sub.Open();
pub.Open();
sub.Subscribe("busytime", (queue,payload) =>
{
// you don't actually need the payload, probably
var received = Encoding.UTF8.GetString(payload);
Console.WriteLine("Work to do! Look busy!: " + received);
});
string line;
Console.WriteLine("Enter messages to send, or q to quit");
while((line = Console.ReadLine()) != null && line != "q")
{
pub.Publish("busytime", line);
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.