【问题标题】:How to check every 2 minutes for report checking in datagrid wpf?如何在datagrid wpf中每2分钟检查一次报告检查?
【发布时间】:2015-04-06 09:46:52
【问题描述】:

我想检查数据库中所有数据的状态是否更改为“已报告”在 wpf datagrid 中自动刷新

这里正在使用下面的代码,告诉我如何在线程中给出它以每 2 分钟自动刷新一次。 如何为自动刷新检查提供以下代码:-

 public void automaticreport(object m)
    {            
        foreach (var autsdyid in LoadStudyIdentifiers())
        {
            if (!this.reportchk)
            {
                Reportnew cf = new Reportnew();
                ThreadPool.QueueUserWorkItem((WaitCallback)(o => cf.ReportRetrive(this, autsdyid, true)));
            }
            else
            {
                int num = (int)System.Windows.Forms.MessageBox.Show("Reports checking in progress, Please wait sometime and try again later", "OPTICS", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            }
        }
    }

    private string[] LoadStudyIdentifiers()
    {
        var results = new List<string>();
        using (var con = new SqlConnection(constr))
        {
            con.Open();
            var autoquery = "Select StudyUID From StudyTable Where status='2'";

            using (var cmd = new SqlCommand(autoquery, con))
            {
                SqlDataReader rdr = cmd.ExecuteReader();
                while (rdr.Read())
                {
                    results.Add(rdr.GetString(rdr.GetOrdinal("StudyUID")));
                }
            }

        }

        return results.ToArray();
    }

【问题讨论】:

  • 我认为在 SO 上下文中解释得太宽泛
  • @KcDoD:如何在线程自动刷新中给它?
  • 这是您需要每 2 分钟运行一次的代码还是您想要实现的目标?
  • 一种选择是使用定时器......并用定时器触发一些事件......
  • 是的,这是代码,如何通过自动在线程中给出 -@ThomasLindvall

标签: c# wpf multithreading windows-services wpfdatagrid


【解决方案1】:

如果您需要以编程方式获取数据,我会这样做:

async void Main()
{
    Foo f = new Foo();
    Task s = await f.Bar(); 
    Console.WriteLine("Done");
}

public class Foo
{
    public Foo()
    {
    }
    //recursive tail function
    public async Task<Task> Bar()
    {
        //enter your code here
        doSomething();
        //Change this to what time you desire
        await Task.Delay(1000);
        return Bar();
    }
}

您也可以使用简单的尾递归将其作为同步函数运行,但它会锁定资源,但您必须考虑到如果您不刷新流并正确处理不同类型的对象,这可能会导致问题。

更安全的路线:

我的另一个想法是安排(计划任务)一个小型工作人员收集数据并将其保存到文档(json、csv、xml w/e),然后向您的应用发送调用以进行更新。

【讨论】:

  • 你可以在 doSomething() 的地方添加代码,不过你需要使用 VS 2012,stackoverflow.com/questions/19423251/… 或在新线程中将其设为同步任务。建议您尝试一下,看看它的作用。
猜你喜欢
  • 2021-06-18
  • 2016-08-11
  • 2012-10-16
  • 1970-01-01
  • 2016-11-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多