如何自动消耗web服务任务 [英] How to automate task of consuming webservice

查看:160
本文介绍了如何自动消耗web服务任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 WinForm应用程序这需要消耗 Web服务。 Web服务检查在数据库中的任何变化。如果在所述的winform应用应该被通知该数据库的任何改变,并相应地执行某些任务。我该怎么办呢?

I have a winform application which needs to consume a web service. Web service checks in the database for any changes. If there are any changes in the database the winform application should be notified and will perform some tasks accordingly. How do I do that?

我想在我的WinForm应用程序后说,每5分钟连接到Web服务,并检查是否有新的变化是在数据库已经完成使用计时器。有没有其他办法呢?

I thought of using timer in my winform application and after say every 5 min connect to a web service and check if new changes are been done in Database. Is there any other way for this?

更新:

我张贴了code此基础上的答案:

I am posting the code here base on the answers:

类PerformTasks     {

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(顶部code段)。 下面表格被打开,但它似乎总是加载。使用时间之前,它用来做工精细。我该如何处理呢?

Now everything works fine except 1 task i.e popMessage(code snippet on top). Here the form is opened but it appears to be loading always. Before using times it used to work fine. How can I handle it?

推荐答案

这是特别是如果Web服务不是基于WCF的唯一途径,或者如果你不能对其进行修改。

That's the only way especially if the web service is not WCF-based or if you can't afford to modify it.

如果你使用一个计时器只是确保你使用System.Timers.Timer的,并按照说明的此处使已用处理程序的UI线程上执行。此外,当计时器滴答你应该产生一个工作线程(或工作,或等待在异步方法),使得服务调用。你不想你的UI被阻塞而服务呼叫正在进行。

If you're using a timer just make sure you use System.Timers.Timer and follow the instructions here so that the Elapsed handler is executed on the UI thread. Moreover, when the timer ticks you should probably spawn a worker thread (or Task, or await on an async method) that makes the service call. You don't want your UI to be blocked while the service call is in progress.

如果您通过网络进行业务控制,那么你可能要探讨的 WCF双面打印服务,让您回调客户端中的服务。

If you have control over the web service, then you may want to explore WCF Duplex Services, which allow you to callback clients from within services.

这篇关于如何自动消耗web服务任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆