我在我的任务管理器中注意到我有这个应用程序的几个副本 - 虽然没有任何CPU资源。我如何正确地在C#中退出/停止/处置控制台应用程序
我知道我必须做一些错误的,所以我想请问集体...
这里是无菌代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Diagnostics;
namespace AnalyticsAggregator
{
class Program
{
[STAThread]
static void Main(string[] args)
{
try
{
bool onlyInstance = false;
Mutex mutex = new Mutex(true, "AnalyticsAggregator", out onlyInstance);
if (!onlyInstance)
{
return;
}
"Do stuff with the database"
GC.KeepAlive(mutex);
}
catch (Exception e)
{
EventLog eventlog = new EventLog("Application");
eventlog.Source = "AnalyticsAggregator";
eventlog.WriteEntry(e.Message, EventLogEntryType.Error);
}
}
}
}
}
我有其他的控制台应用程序是不是互斥/单是展出同样的行为,我做错了什么?我假设某种类型的处置......
感谢
为什么你在那里有'GC.KeepAlive(互斥体)'? – feralin 2013-03-25 16:20:19
我将你的代码复制并粘贴到Visual Studio中,并用长'Thread.Sleep(100000)'代替''Do Stuff''行。它按预期工作,只有一个应用程序实例正在运行。你能发布可以复制问题的代码,以及关于你的环境的更多信息吗? – 2013-03-25 16:29:14
我见过的,然后用过的例子有GC.KeepAlive(互斥体); 这是错的吗? – 2013-03-25 16:59:09