The application just closes (no errors or smth) when starting from VS2010

The problem is that the application closes without any errors, VS remains open. I have several dynamically created ones FileSystemWatchers, all of them have an event handler in the "Created" event. Therefore, this event handler method is as follows:

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
    }
}

The line that causes the program to close is the one where I add the file name in. DataGridView - filesDataGrid.Rows.Add(f1.Name); Also works OK without this line. It is strange that the application starts normally when it is launched from an .exe file in the projects folder. I do not see an error in my code, but I think that something is terribly wrong with this if it does not even display an error message. And - what are the most common reasons why a program could simply shut down without warning?

+3
source share
2 answers

FileSystemWatchertriggers events in a separate thread. The logic inside the event handlers must take this fact into account and perform any necessary synchronization. So you will 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
        }
    }
}
+2
source

Try {} catch (Exception ex) {}. , , DataGridRow FileSystemWatcher.

DataGridViewRow row = filesDataGrid.NewRow();

row["columnname"] = f1.name;

filesDataGrid.Rows.Add(row);
0

Source: https://habr.com/ru/post/1796308/


All Articles