I wanna show a dialog everytime some file has been changed... But everytime that dialog shows up, my app freeze. How can I do this with another thread? Any ideas?
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
// 开发者_JS百科Possivelmente atualizar o número de interações realizadas
Console.WriteLine ("Janela modal destruída");
};
}
The problem is that you are already using another thread. Try one of the following approaches
- Set the
FileSystemWatcher.SynchronizingObject
property so it raises events on your UI thread. Now you can show a UI that won't freeze or - Use
Control.BeginInvoke()
in the event handler.
This was a psychic debugging attempt, there was nothing in your question that helped me be sure that's the correct answer.
Call Show()
instead of ShowDialog()
in your form.
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.show.aspx
Edit - MessageBox classes are Modal, if you want a modeless dialog window you'll have to create it yourself.
精彩评论