gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 14:42:09 25 4
gpt4 key购买 nike

我有一个奇怪的情况,在我关闭主窗体后我的应用程序进程仍然在内存中徘徊,我的 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());
}
}
}

最佳答案

您应该只有一个 Application.Run 以确保当前线程上只有一个消息循环并避免您所描述的那种问题。

关于C# 应用程序未正确关闭(窗体关闭后仍在内存中运行),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6364937/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com