从VS2010运行时,应用程序只是关闭(没有错误或水木清华)水木清华、应用程序、错误

由网友(情话烫耳)分享简介:问题是,应用程序没有任何错误关闭,VS保持打开。我有多个动态创建 FileSystemWatchers ,个个都对事件处理程序创建活动。所以这个事件处理程序的方法是这样的:Problem is that application closes without any error, VS stays opened.I...

问题是,应用程序没有任何错误关闭,VS保持打开。 我有多个动态创建 FileSystemWatchers ,个个都对事件处理程序创建活动。所以这个事件处理程序的方法是这样的:

Problem is that application closes without any error, VS stays opened. I have multiple dynamically created FileSystemWatchers, all of them have eventhandler on "Created" event. So this eventhandler method looks like this :

void watcher_FileCreated(object sender, FileSystemEventArgs e)
{
    FileInfo f1 = new FileInfo(e.FullPath);
    filesDataGrid.Rows.Add(f1.Name);
    foreach (TLPclass table in parameterForm.getParameters)
    {
       //uses some funcion form another class
    }
}

行,导致程序关闭是一个在那里我将文件名的DataGridView - filesDataGrid.Rows.Add(f1.Name); 还没有运行该行确定。 奇怪的是,应用程序运行正常,从.exe文件在项目文件夹中启动时。我看不到错误在我的code,但我想有件事非常不对的地方,如果它甚至不显示错误信息。 而且 - 是什么,为什么程序可能只是关闭,没有警告的最常见原因。

Line which causes program to close is the one where I'm adding File name to DataGridView - filesDataGrid.Rows.Add(f1.Name); Also runs OK without that line. Weird thing is that application runs normally, when launched from .exe file in projects folder. I can't see error in my code, but I guess theres something awfully wrong with it, if it doesn't even show error message. And - what are the most common reasons why program could just shut down with no warnings?

推荐答案

在一个单独的线程的 FileSystemWatcher的将触发事件。在事件处理程序中的逻辑将需要采取这一事实考虑并执行所需的任何同步。所以,你需要这样的:

The FileSystemWatcher will trigger the events in a separate thread. The logic inside the event handlers will need to take that fact in consideration and perform any synchronization needed. So you'll need something like this:

private void watcher_FileCreated(object sender, FileSystemEventArgs e)
{
    if (filesDataGrid.InvokeRequired)
    {
        filesDataGrid.Invoke((MethodInvoker)delegate { watcher_FileCreated(sender, e); });
    }
    else
    {
        FileInfo f1 = new FileInfo(e.FullPath);
        filesDataGrid.Rows.Add(f1.Name);
        foreach (TLPclass table in parameterForm.getParameters)
        {
           //uses some funcion form another class
        }
    }
}
阅读全文

相关推荐

最新文章