2010-11-22 39 views
0

我想每次顯示一個對話框時,一些文件已被更改...但每當該對話框顯示,我的應用程序凍結。我怎樣才能做到這一點與另一個線程?有任何想法嗎?FileSystemWatcher +對話框

protected virtual void CreateWatcher (object path) 
{ 
    if (watcher != null) 
    { 
     watcher.EnableRaisingEvents = false; 
     watcher.Dispose(); 
    } 

    //Create a new FileSystemWatcher. 
    watcher = new FileSystemWatcher(); 

    //Set the filter to only catch TXT files. 
    watcher.Filter = "*.txt"; 
    watcher.IncludeSubdirectories = true; 
    watcher.NotifyFilter = NotifyFilters.LastWrite; 

    //Subscribe to the Created event. 
    watcher.Changed += new FileSystemEventHandler (OnChanged); 
    watcher.Created += new FileSystemEventHandler (OnChanged); 
    //watcher.Deleted += new FileSystemEventHandler (OnChanged); 
    //watcher.Renamed += new RenamedEventHandler (OnRenamed); 

    //Set the path to C:\\Temp\\ 
    watcher.Path = @path.ToString(); 

    //Enable the FileSystemWatcher events. 
    watcher.EnableRaisingEvents = true; 
} 
void OnChanged (object source, FileSystemEventArgs e) 
{ 
    NovaInteracaoMsg(); 
    } 

protected virtual void NovaInteracaoMsg() 
{ 
    novaInteracao = new MessageDialog (this, DialogFlags.DestroyWithParent, MessageType.Question, ButtonsType.YesNo, "Foi detectada a mudança nos arquivos do modelo. Deseja inserir uma nova interação?"); 
    ResponseType result = (ResponseType)novaInteracao.Run(); 

    if (result == ResponseType.Yes) { 
     OpenInfoWindow (novaInteracaoPath); 
     return; 
    } 
    else { 
     novaInteracao.Destroy(); 
    } 
} 

void OnRenamed (object source, RenamedEventArgs e) 
{ 
    //Console.WriteLine ("File: {0} renamed to\n{1}", e.OldFullPath, e.FullPath); 
    } 

protected virtual void OpenInfoWindow (string path) 
{ 
    ModMemory.Iteration iterWin = new ModMemory.Iteration (path); 
    iterWin.Modal = true; 
    iterWin.Show(); 

    iterWin.Destroyed += delegate { 
     // TODO: Funções para executar quando a janela for fechada 
     // Possivelmente atualizar o número de interações realizadas 
     Console.WriteLine ("Janela modal destruída");   
    }; 
} 
+0

那個對話框是什麼? MessageBox.Show? – 2010-11-22 19:51:33

回答

5

問題是你已經在使用另一個線程了。請嘗試以下的方法一個

  1. 設置FileSystemWatcher.SynchronizingObject屬性,因此它 提高你的UI線程事件。現在您可以顯示不會凍結的界面 或
  2. 在事件處理程序中使用Control.BeginInvoke()

這是一個心理調試嘗試,你的問題中沒有任何東西可以幫助我確定這是正確的答案。