我有一个奇怪的情况,我关闭主表单后,我的应用程序进程仍然在内存中存在,我的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());
}
}
}
答案 0 :(得分:5)
您应该只有一个Application.Run,以确保当前线程上只有一个消息循环,并避免您描述的那种问题。
答案 1 :(得分:2)
这通常表示未终止的后台主题。
答案 2 :(得分:1)
如果锁定文件不存在,则会运行新的“主窗体”。我猜Form1在运行时是一个隐藏的形式。
答案 3 :(得分:1)
我已经解决了我的情况。有关详细信息,请阅读this article。
我将关键部分放入自己的线程中。该主题设置为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
此致