用于数据库循环轮询的C#计时器

用于数据库循环轮询的C#计时器,c#,sql-server,wpf,database,timer,C#,Sql Server,Wpf,Database,Timer,到目前为止,我对C#还不是很熟悉,至少在选择第一次做某事的方式时,我还没有足够的信心 从我的WPF应用程序组织SQL Server数据库基于计时器的轮询以更新控件的最佳方法是什么 你能分享一些关于这个问题的想法吗,或者,也许,代码示例,它是如何实现的?在多大程度上可以(或合理)缩短此类投票的时间?做什么可能是危险的 也许有一种标准且正确的方法可以做到这一点?您可以使用对象,尽管它将您的实现与SQL Server 2005+紧密地联系在一起。您的需求描述得非常笼统,因此我只能笼统地回答: 一般来说

到目前为止,我对C#还不是很熟悉,至少在选择第一次做某事的方式时,我还没有足够的信心

从我的WPF应用程序组织SQL Server数据库基于计时器的轮询以更新控件的最佳方法是什么

你能分享一些关于这个问题的想法吗,或者,也许,代码示例,它是如何实现的?在多大程度上可以(或合理)缩短此类投票的时间?做什么可能是危险的


也许有一种标准且正确的方法可以做到这一点?

您可以使用对象,尽管它将您的实现与SQL Server 2005+紧密地联系在一起。

您的需求描述得非常笼统,因此我只能笼统地回答:

一般来说:

  • 将同时轮询多少个应用程序
  • 结果集有多大
  • 十个客户不算多,一万个才算多。十行很小,一千行又大又慢。回答这些问题将帮助您了解如何设置轮询频率


    您需要平衡其他考虑因素,如UI响应性(异步更新数据)、对新数据的需求以及数据库更新频率的了解,以确定最有效的设计。

    如果您需要应用程序范围的更新,并且不会造成太多的延迟,最好的选择是启动一个后台
    线程
    ,该线程可以运行事件并向订阅服务器发布数据已刷新的事件(可能首先通过向事件添加自定义
    EventArgs
    来检查是否也使用了某些特定逻辑对其进行了更改)

    例如,在顶级(可能是您的主表单代码?)的某个位置创建此类的实例:

    订阅每个需要响应
    UpdateFinished
    事件新更新的区域。这将在用于构造
    PollerThread
    类的线程上执行


    这个答案听起来可能有点松散,并且更具体地适用于windows窗体项目,但其有用性实际上取决于您当前的实现。无论如何,至少你可以在这个基础上再接再厉。希望能有帮助:)

    昨晚我突然想起我忘了在这里提到什么。不要忘记在窗体关闭时取消分配窗体的事件侦听器,例如:
    myPollerThread.UpdateFinished-=neweventhandler(myPollerThread_UpdateFinished)否则关闭的表单仍将接收事件,并且很可能导致正在处理的表单出现一些异常。
    
    using System;
    using System.Threading;
    using System.Windows.Forms;
    
    public class PollerThread
    {
        private Thread _thread;
        private SynchronizationContext _syncContext;
    
        public event EventHandler UpdateFinished;
    
        public PollerThread()
        {
            _syncContext = WindowsFormsSynchronizationContext.Current;
    
            ThreadStart threadStart = new ThreadStart(ThreadStartMethod);
            _thread = new Thread(threadStart);
            _thread.Name = "UpdateThread";
            _thread.IsBackground = true;
            _thread.Priority = System.Threading.ThreadPriority.Normal;
        }
    
        public void Start()
        {
            if ((_thread.ThreadState & ThreadState.Unstarted) == ThreadState.Unstarted)
                _thread.Start();
            else
                throw new Exception("Thread has already been started and may have completed already.");
        }
    
        public void ThreadStartMethod()
        {
            try
            {
                while (true)
                {
                    // Go get the new data from the SQL server
    
                    OnUpdateFinished(); // Notify all subscribers (on their own threads)
                    Thread.Sleep(10000); // 10 sec wait before the next update
                }
            }
            catch (ThreadAbortException)
            {
                // The thread was aborted... ignore this exception if it's safe to do so
            }
        }
    
        protected virtual void OnUpdateFinished()
        {
            if (UpdateFinished != null)
            {
                SendOrPostCallback method = new SendOrPostCallback(
                delegate(object state)
                {
                    UpdateFinished(this, EventArgs.Empty);
                });
                _syncContext.Send(method, null);
            }
        }
    }