求教.net compact framework 上实现双击exe文件只出一个实例的问题

forestwind 2009-09-26 03:49:44
我的问题是如何在.net compact framework 框架下实现双击exe文件只出一个实例,在普通.net framework上实现这个没有问题,以下是代码

[MTAThread]
static void Main()
{
Process instance = RunningInstance();
if (instance == null)
{
//There isn't another instance, show our form.
Application.Run(new Form1());
}
else
{
//There is another instance of this process.
HandleRunningInstance(instance);

}

//Application.Run(new Form1());
}

public static Process RunningInstance()
{
Process current = Process.GetCurrentProcess();

Process[] processes = Process.GetProcessesByName (current.ProcessName);
// Process[] processes = Process.GetProcessById(current.Id);
//Loop through the running processes in with the same name
foreach (Process process in processes)
{
//Ignore the current process
if (process.Id != current.Id)
{
//Make sure that the process is running from the exe file.
if (Assembly.GetExecutingAssembly().Location.Replace("/", "\\") == current.MainModule.FileName)
{
//Return the other process instance.
return process;
}
}
}//foreach

}//runninginstance

可是在.net compact framework框架下没有GetProcessesByName,Assembly.GetExecutingAssembly().Location和current.MainModule这个功能,可是我又找不到替代,诚心求教大牛给予指点
...全文
101 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
forestwind 2009-09-28
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 huwei001982 的回复:]
和 winform 一样, 用 Mutex 啊, google 一下
[/Quote]

搜过了,主要问题是compact framework 的 mutex 构造函数不支持三个参数,没有了第三个bool, 没法判断啊
huwei001982 2009-09-27
  • 打赏
  • 举报
回复
和 winform 一样, 用 Mutex 啊, google 一下
forestwind 2009-09-27
  • 打赏
  • 举报
回复
回复3楼跟4楼的兄弟,你们调用的函数compact framework 里面不包括啊
sohighthesky 2009-09-26
  • 打赏
  • 举报
回复
这里WinForm下的,你看可不可以,手头没环境不好测试,
另外WinCE应该有FindWindow这个API吧,查找窗口类或名称固定的,
这样就能判断了

static void Main()
{
bool isRun;
System.Threading.Mutex mu = new System.Threading.Mutex(true, "OnlyRunOneInstance", out isRun);
if (isRun)
{

Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
mu.ReleaseMutex();
}
else
{
MessageBox.Show("程序已经运行!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
风之影子 2009-09-26
  • 打赏
  • 举报
回复
 static void Main()
{
int iProcessNum = 0;

foreach (Process singleProc in Process.GetProcesses())
{
if (singleProc.ProcessName == Process.GetCurrentProcess().ProcessName)
{
iProcessNum += 1;
}
}
if (iProcessNum == 2)
{
LitianClass.MessageBoxOK("管理系统\n\n已经在运行中……");
}
else if (iProcessNum > 2)
{
return;
}
else
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new login());
}
}
zcandyly20211 2009-09-26
  • 打赏
  • 举报
回复
UP
cg2003 2009-09-26
  • 打赏
  • 举报
回复
up

110,566

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧