从VS2010运行时,应用程序只是关闭(没有错误或水木清华) [英] Application just closes (no error or smth) when run from VS2010

查看:159
本文介绍了从VS2010运行时,应用程序只是关闭(没有错误或水木清华)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题是,应用程序没有任何错误关闭,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
        }
    }
}

这篇关于从VS2010运行时,应用程序只是关闭(没有错误或水木清华)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆