C # application does not work correctly (still executed in memory after closing the form)

I have a strange situation where my application process is still lingering in memory after I close my main form, my Program.cs code is below:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.IO;

namespace WindowsFormsApplication1
{
    static class Program
    {    
        [Flags]
        enum MoveFileFlags
        {
            None = 0,
            ReplaceExisting = 1,
            CopyAllowed = 2,
            DelayUntilReboot = 4,
            WriteThrough = 8,
            CreateHardlink = 16,
            FailIfNotTrackable = 32,
        }

        [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
        static extern bool MoveFileEx(
            string lpExistingFileName,
            string lpNewFileName,
            MoveFileFlags dwFlags
        );

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
        string lockFile = "run.dat";
        if (!File.Exists(lockFile))
        {
            // that a first run after the reboot => create the file
            File.WriteAllText(lockFile, "");

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form2());
        }
        else
        {
            // that a consecutive run      
        }
          Application.Run(new Form1());
        }
    }
}
+3
source share
4 answers

You should have only one Application.Run to ensure only one message loop in the current thread and avoid the described problem.

+5
source

This usually indicates a background thread that has not completed.

+2
source

, " ". , Form1 .

+1

. . .

. Thread.IsBackground = True DotNet .

Dim thStart As New System.Threading.ParameterizedThreadStart(AddressOf UpdateImageInGuiAsync)
Dim th As New System.Threading.Thread(thStart)
th.Start() 
th.IsBackground = True

Background thread

+1

All Articles