如何自动化使用Web服务的任务

我有一个winform应用程序需要使用Web服务 。 Web服务在数据库中检查是否有任何更改。 如果数据库中有任何更改,则应通知winform应用程序并相应地执行某些任务。 我怎么做?

我想在我的winform应用程序中使用计时器,然后说每5分钟连接一个Web服务并检查数据库中是否进行了新的更改。 还有其他办法吗?

更新:

我在这里根据答案发布代码:

class PerformTasks {

public static bool checkIfInProgress { get; set; } public static void InitializeWebService() { try { Timer = new System.Timers.Timer(2000); Timer.Elapsed += OnTimedEvent; Timer.Enabled = true; } } private static void callService() { using (var service = new WebServiceSoapClient()) { checkIfInProgress = true; task1(); task2(); popMessage(); checkIfInProgress = false; } } private static void OnTimedEvent(Object source, ElapsedEventArgs e) { if (checkIfInProgress == false) { callService(); } } private static void PpopMessage() { var form = new Form { StartPosition = FormStartPosition.Manual, ShowInTaskbar = false, Text = "Message", Size = new Size(500, 200), }; var screen = Screen.FromPoint(form.Location); Label lblText = new Label(); lblText.Text ="Test Text"; lblText.Dock = DockStyle.Fill; lblText.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; form.MaximizeBox = false; form.Controls.Add(lblText); form.Location = new Point(screen.WorkingArea.Right - form.Width, screen.WorkingArea.Bottom - form.Height); form.Show(); } 

现在一切正常,除了1个任务,即popMessage(顶部的代码片段)。 这里表单打开但似乎总是加载。 在使用时间之前它曾经很好地工作。 我该怎么处理?

这是唯一的方法,特别是如果Web服务不是基于WCF或者您无法负担修改它。

如果您正在使用计时器,请确保使用System.Timers.Timer并按照此处的说明操作,以便在UI线程上执行Elapsed处理程序。 此外,当计时器滴答时,您可能应该生成一个工作线程(或任务,或等待异步方法)进行服务调用。 您不希望在服务调用过程中阻止您的UI。

如果您可以控制Web服务,那么您可能希望探索WCF双工服务 ,它允许您从服务中回调客户端。

SignalR允许您实时实现Web服务(不需要计时器或更新之间的延迟)。 它允许您在客户端和服务器之间建立持久连接,然后服务器可以使用一堆传输在任何时候向客户端发送消息; WebSockets,Server Sent Events,Forever Frame和Long Polling)基于该订单中提供的支持。

您可以使用SignalR建立连接,并在服务器上发生某些事情时(例如您提到的数据库中的更改)向需要通知的所有客户端广播。 例如。

 Clients.All.notifyDatabaseChanged(args);