【发布时间】:2014-09-17 01:27:52
【问题描述】:
我有一个 winform 应用程序,它需要使用 Web 服务。 Web 服务检查数据库中的任何更改。如果数据库中有任何更改,应该通知 winform 应用程序并相应地执行一些任务。我该怎么做?
我想在我的 winform 应用程序中使用计时器,然后说每 5 分钟连接到一个 Web 服务并检查数据库中是否进行了新的更改。有没有其他办法呢?
更新:
我根据答案在这里发布代码:
类 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(顶部的代码 sn-p)。 此处表单已打开,但似乎总是在加载。在使用时间之前,它曾经可以正常工作。我该如何处理?
【问题讨论】:
-
计时器适用于 WinForms。
标签: c# .net winforms web-services polling