2015-11-04 93 views
2

我想經常檢查一個xml文件是否已更新...我想要創建一個函數,即使它是每秒鐘的四分之一..如何不斷調用WPF應用程序中的函數

private void button_Click(object sender, RoutedEventArgs e) 
    { 
     while (true) 
     { 
      textBox.Text = "Count" + Count++; 
     } 
    } 
+0

也許這將幫助你... HTTP://www.codeproject.com/Articles/26875/WPF的XmlDataProvider - 雙向-數據綁定 – Jose

回答

4

就可以輕鬆實現,使用async/await

public async void Poll() 
{ 
    while(true) 
    { 
     textBox.Text = "Count" + Count++; 
     await Task.Delay(TimeSpan.FromMilliseconds(250); 
    } 
} 

提供取消選項提供CancellationToken

while(!cancellationToken.IsCancellationRequested) 

欲瞭解更多信息請參見CancellationToken

1

的文檔沒有投票,你可以警告的文件系統的修改,包括與FileSystemWatcher子目錄。

4類事件:
- 創建
- 更改
- 刪除
- 更名

FileSystemWatcher watcher = new FileSystemWatcher(path); 
watcher.IncludeSubdirectories = true; 
watcher.Filter = "*.config"; 
watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite 
    | NotifyFilters.FileName | NotifyFilters.DirectoryName; 
watcher.EnableRaisingEvents = true; 
watcher.Changed += new FileSystemEventHandler(OnChanged); 
watcher.Created += new FileSystemEventHandler(OnCreated); 
watcher.Deleted += new FileSystemEventHandler(OnDeleted); 
watcher.Renamed += new RenamedEventHandler(OnRenamed); 

void OnChanged(object source, FileSystemEventArgs e) 
{ 
    // Specify what is done when a file is changed, created, or deleted. 
    Console.WriteLine("File: " + e.FullPath + " " + e.ChangeType); 
} 
相關問題