如何每2分钟检查一下datagrid wpf中的报表检查?

How to check every 2 minutes for report checking in datagrid wpf?
2020-11-21
  •  译文(汉语)
  •  原文(英语)

我想检查数据库中所有数据的状态是否更改为"已报告",然后在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();
    }
速聊1:
我认为在SO上下文中无法解释
速聊2:
:如何在自动线程刷新中给它?
速聊3:
这是您需要每2分钟运行一次的代码还是您想要实现的目标?
速聊4:
一种选择是使用计时器..并使用计时器触发一些事件.
速聊5:
是的,这是代码,如何通过-@ ThomasLindvall自动在线程中给出
解决过程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)中,然后向您的应用程序发送调用以进行更新.

速聊1:
您可以将代码添加到doSomething()处,不过您需要使用VS 2012stackoverflow.com/questions/19423251/…或使其成为新线程中的同步任务.建议您尝试一下,看看它能做什么.

I want to check all the datas in database for the status is changed to "Reported" automatically refresh it in wpf datagrid

Here the below code am using,stell me how to given this in a thread for automatically refresh every 2minutes. how to give the below code for automatic refresh checking:-

 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();
    }
Talk1:
I think it's too broad to explain in SO context
Talk2:
:How to given it in automatic thread refresh?
Talk3:
Is this the code you need to run every 2 mins or what do you want to achieve?
Talk4:
one option is to use timers.. and trigger some event with timer..
Talk5:
Yes this is the code,how to given in a thread via automatically -@ThomasLindvall
Solutions1

If you need to get the data programmatically I would do something like this:

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();
    }
}

You could also run it as a synchronous function with a simple tail recursion it will lock up resources though, you have to take in consideration that this might cause problems if you don't flush streams and dispose of different types objects correctly.

The safer route:

My other idea would be to schedule (Scheduled Task) a small worker that collects the data and saves it down to a document (json, csv, xml w/e) and then send a call to your app to update.

Talk1:
You can add the code where it says doSomething(), you'll need to use VS 2012 though, stackoverflow.com/questions/19423251/… or make it a synchronous task in a new thread. Would recommend that you try this out and see what it does.
转载于:https://stackoverflow.com/questions/28362589/how-to-check-every-2-minutes-for-report-checking-in-datagrid-wpf

本人是.net程序员,因为英语不行,使用工具翻译,希望对有需要的人有所帮助
如果本文质量不好,还请谅解,毕竟这些操作还是比较费时的,英语较好的可以看原文

留言回复
我们只提供高质量资源,素材,源码,坚持 下了就能用 原则,让客户花了钱觉得值
上班时间 : 周一至周五9:00-17:30 期待您的加入