C# FileSystemWatcher+;对话
我想在每次更改某些文件时显示一个对话框。。。但每次对话框出现时,我的应用程序都会冻结。我怎样才能用另一个线程来实现这一点?有什么想法吗C# FileSystemWatcher+;对话,c#,multithreading,C#,Multithreading,我想在每次更改某些文件时显示一个对话框。。。但每次对话框出现时,我的应用程序都会冻结。我怎样才能用另一个线程来实现这一点?有什么想法吗 protected virtual void CreateWatcher (object path) { if (watcher != null) { watcher.EnableRaisingEvents = false; watcher.Dispose (); } //Create a new
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");
};
}
在表单中调用Show()
而不是ShowDialog()
Edit-MessageBox类是模态的,如果您想要一个无模态的对话框窗口,您必须自己创建它。调用Show()
,而不是表单中的ShowDialog()
编辑-MessageBox类是模态的,如果您想要一个非模态的对话框窗口,您必须自己创建它。问题是您已经在使用另一个线程。尝试以下方法之一
FileSystemWatcher.SynchronizingObject
属性,使其
在UI线程上引发事件。现在,您可以显示一个不会
冻结或这是一次心理调试尝试,您的问题中没有任何内容可以帮助我确定答案是否正确。问题是您已经在使用另一个线程。尝试以下方法之一
FileSystemWatcher.SynchronizingObject
属性,使其
在UI线程上引发事件。现在,您可以显示一个不会
冻结或这是一次心理调试尝试,你的问题中没有任何东西能帮助我确定这是正确的答案。那个对话框是什么?MessageBox.Show?再详细一点,一些代码(可能是检测到更改和显示消息框的地方等)会很好。我已经编辑以显示代码。。。对不起…那个对话是什么?MessageBox.Show?再详细一点,一些代码(可能是检测到更改和显示消息框的地方等)会很好。我已经编辑以显示代码。。。很抱歉