我遇到了一个奇怪的情况,即在关闭主窗体后,我的应用程序进程仍然停留在内存中。以下是我的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());
}
}
}