C#应用程序未正确关闭(窗体关闭后仍在内存中运行)

C#应用程序未正确关闭(窗体关闭后仍在内存中运行),c#,.net,windows,winforms,C#,.net,Windows,Winforms,我有一个奇怪的情况,在我关闭主窗体后,我的应用程序进程仍然停留在内存中,我的Program.cs代码如下: using System; using System.Collections.Generic; using System.Linq; using System.Windows.Forms; using System.Runtime.InteropServices; using System.IO; namespace WindowsFormsApplication1 { stat

我有一个奇怪的情况,在我关闭主窗体后,我的应用程序进程仍然停留在内存中,我的Program.cs代码如下:

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's a first run after the reboot => create the file
            File.WriteAllText(lockFile, "");

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form2());
        }
        else
        {
            // that's a consecutive run      
        }
          Application.Run(new Form1());
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Windows.Forms;
使用System.Runtime.InteropServices;
使用System.IO;
命名空间Windows窗体应用程序1
{
静态类程序
{    
[旗帜]
枚举移动文件标志
{
无=0,
ReplaceExisting=1,
CopyAllowed=2,
DelayUntilReboot=4,
WriteThrough=8,
CreateHardlink=16,
FailifNotTracable=32,
}
[DllImport(“kernel32.dll”,SetLastError=true,CharSet=CharSet.Unicode)]
静态外部bool MoveFileEx(
字符串lpExistingFileName,
字符串lpNewFileName,
MoveFileFlags dwFlags
);
/// 
///应用程序的主要入口点。
/// 
[状态线程]
静态void Main()
{
字符串lockFile=“run.dat”;
如果(!File.Exists(lockFile))
{
//这是重新启动后的第一次运行=>创建文件
writealText(lockFile,“”);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(新Form2());
}
其他的
{
//这是一个连续的运行
}
Application.Run(新Form1());
}
}
}

这通常表示后台线程尚未终止。

您应该只有一个应用程序。运行以确保当前线程上只有一个消息循环,并避免您描述的问题。

如果锁文件不存在,您将运行一个新的“主窗体”。我猜Form1在运行时是一个隐藏的表单。

我已经解决了我的问题。详情请阅读

我把关键部分放到自己的线程中。线程设置为
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

关于

您的应用程序是否创建了任何后台工作线程?我在这里有相同的情况。使用线程。但是一个线程需要一个“Application.IsShuttingDown”属性,但是除了ApplicationExit事件之外没有其他属性,但是该类无法在每次调用时添加此事件。是的,如果条件正确,看起来您正在运行两个表单(尽管代码缩进不好会掩盖这一点)。这绝对是不标准的。