kubilay bayraktar
kubilay bayraktar

Reputation: 101

C# application is closed unexpectedly!

I'm having a very frustrating problem. I have a c# win application. When I have clicked the button, the program closes itself after executed the click event handler. Even if I have debugged the code unfortunately I can't see any error, It just quits the program.

Where am I going wrong?

Here is the Code:

private void btnOpenFolder_Click(object sender, EventArgs e)
    {
            DialogResult dg = fd1.ShowDialog();
            if (dg == DialogResult.OK)
            {
                lblInput.Text = fd1.SelectedPath;
                btnOpenFolder.Enabled = false;
                timerCallback = new TimerCallback(tmrQualityEvent);
                tmrQuality = new System.Threading.Timer(timerCallback, null, 0, 1000);
                Thread qualityThread = new Thread(new ThreadStart(QualityMapOpenFolder));
                qualityThread.Start();
                QualityMapOpenFolder();
            }
    }

void QualityMapOpenFolder()
    {
        fileList.Clear();
        string path = lblInput.Text;
        if (Directory.Exists(path))
        {
            foreach (var file in Directory.GetFiles(path))
            {
                if (Path.GetExtension(file) != ".kml")
                {
                    fileList.Add(file);
                }
            }
            SetProgressBarValue(0);
            ChangeFileNameLabel(fileList[0]);
            FileName = fileList[0];
        }
        else
            SetText("Please make sure you have correctly set the open folder path!", true);

        dataListQuality = GetInputData();

        SetText("Calculated Data has been created, please click process files...", false);
        SetProcessButtonStatus(true);
    }

Upvotes: 0

Views: 2907

Answers (2)

ChrisWue
ChrisWue

Reputation: 19030

Attach an event handler to the UnhandledException handler and log it. Should help you to find out why your application is crashing.

Update: Now that you have posted some code:

  1. You seem to update UI elements from another thread which you start. You should access UI components only from the thread on which they were created (usually the main thread). Consider using a BackgroundWorker
  2. You start the QualityMapOpenFolder method on a thread and then you also call it after you started the thread - this seems a bit weird and has probably some unexpected side effects.

Upvotes: 3

elder_george
elder_george

Reputation: 7879

The common reason for this kind of behavior is unhandled exception in background thread. To prevent program.

@ChrisWue wrote on how to detect this kind of exceptions.

Also, often Windows Application log provides an insight on unhandled errors.

See here how to prevent killing app in this case.

Upvotes: 0

Related Questions