2015-02-06 21 views
-2

我想檢查數據庫中的所有數據爲狀態更改爲「報告」自動刷新它在wpf datagrid如何檢查每2分鐘報告檢查datagrid wpf?

在這裏下面的代碼正在使用,stell我如何給這個在一個線程中自動刷新每一個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(); 
    } 
+0

我認爲這是太寬泛來解釋在SO上下文 – 2015-02-06 09:36:10

+0

@KcDoD:如何給它在自動線程刷新? – NvadeepKumar 2015-02-06 09:37:37

+0

這是您需要每2分鐘運行一次的代碼還是您想實現的目標? – 2015-02-06 09:39:06

回答

0

如果你需要得到的數據編程我會做這樣的事情:

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瓦特/ e)和然後發送電話到您的應用程序進行更新。

+0

你可以添加代碼說doSomething(),你需要使用VS 2012,雖然,http://stackoverflow.com/questions/19423251/in-net-4-0-i-have-not -avaliable-async-await-keywords或在新線程中將其設爲同步任務。建議你嘗試一下,看看它有什麼作用。 – 2015-02-09 07:42:37