惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

D
Docker
爱范儿
爱范儿
T
The Exploit Database - CXSecurity.com
量子位
T
Tailwind CSS Blog
T
Threatpost
The GitHub Blog
The GitHub Blog
AWS News Blog
AWS News Blog
云风的 BLOG
云风的 BLOG
K
Kaspersky official blog
P
Proofpoint News Feed
博客园 - 司徒正美
L
LangChain Blog
T
Threat Research - Cisco Blogs
C
CERT Recently Published Vulnerability Notes
罗磊的独立博客
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - 叶小钗
S
Secure Thoughts
The Last Watchdog
The Last Watchdog
Spread Privacy
Spread Privacy
H
Hacker News: Front Page
T
Troy Hunt's Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Google DeepMind News
Google DeepMind News
W
WeLiveSecurity
A
Arctic Wolf
Apple Machine Learning Research
Apple Machine Learning Research
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
P
Proofpoint News Feed
T
Tor Project blog
T
The Blog of Author Tim Ferriss
I
Intezer
P
Privacy & Cybersecurity Law Blog
美团技术团队
N
Netflix TechBlog - Medium
博客园_首页
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
V
Vulnerabilities – Threatpost
Application and Cybersecurity Blog
Application and Cybersecurity Blog
G
Google Developers Blog
Attack and Defense Labs
Attack and Defense Labs
T
Tenable Blog
月光博客
月光博客
Stack Overflow Blog
Stack Overflow Blog
J
Java Code Geeks
腾讯CDC
Microsoft Security Blog
Microsoft Security Blog
A
About on SuperTechFans
Last Week in AI
Last Week in AI

博客园 - Sangplus

extern alias 英语面试常用语 【转】FLASH轮换广告源码 走马灯的js代码 让Enumerable可以具有each方式的语法。 C#之扩展方法学习 使用非托管代码进行字节数组的快速拷贝 c#复习——虚方法、重写方法和抽象方法(官方解释) vs2005工具栏重置 一种实用的页面传参方法 所有HTTP状态代码及其定义 ASP.NET验证控件祥解[转] C#制作WinForm控件[转] 关于web.config的读写管理 关于SqlServer2005开发者版不同电脑之间的连接问题 JavaScript中有时候需要给this起一个别名。 不知不觉代码已经超过1万行了 关于RequiredFieldValidator的怪异现象 javascript添加下拉列表和速度有关系吗?
[转]C#单进程解决方案
Sangplus · 2011-04-22 · via 博客园 - Sangplus

原文地址:http://hi.baidu.com/wth123/blog/item/86a3f50ceeb754e937d1226c.html

一、使用用互斥量(System.Threading.Mutex)

  同步基元,它只向一个线程授予对共享资源的独占访问权。在程序启动时候,请求一个互斥体,如果能获取对指定互斥的访问权,就职运行一个实例。

  代码

  bool createNew;

  using (System.Threading.Mutex mutex =new System.Threading.Mutex(true, Application.ProductName, out createNew))

  {

  if (createNew)

  {

  Application.Run(new Form1());

  }

  else

  {

  MessageBox.Show("应用程序已经在运行中...")

  System.Threading.Thread.Sleep(1000);

  System.Environment.Exit(1);

  }

  }

  二、使用进程名

  代码

  Process[] processes = System.Diagnostics.Process.GetProcessesByName(Appl ication.CompanyName);

  if (processes.Length >1)

  {

  MessageBox.Show("应用程序已经在运行中。。");

  Thread.Sleep(1000);

  System.Environment.Exit(1);

  }

  else

  {

  Application.Run(new Form1());

  }

  三、调用Win32 API,并激活并程序的窗口,显示在最前端

  代码

  /// 该函数设置由不同线程产生的窗口的显示状态

  ///</summary>

  ///<param >窗口句柄</param>

  ///<param >指定窗口如何显示。查看允许值列表,请查阅ShowWlndow函数的说明部分</param>

  ///<returns>如果函数原来可见,返回值为非零;如果函数原来被隐藏,返回值为零</returns>

  [DllImport("User32.dll")]

  privatestaticexternbool ShowWindowAsync(IntPtr hWnd, int cmdShow);

  ///<summary>

  /// 该函数将创建指定窗口的线程设置到前台,并且激活该窗口。键盘输入转向该窗口,并为用户改各种可视的记号。

  /// 系统给创建前台窗口的线程分配的权限稍高于其他线程。

  ///</summary>

  ///<param >将被激活并被调入前台的窗口句柄</param>

  ///<returns>如果窗口设入了前台,返回值为非零;如果窗口未被设入前台,返回值为零</returns>

  [DllImport("User32.dll")]

  privatestaticexternbool SetForegroundWindow(IntPtr hWnd);

  代码

  privateconstint SW_SHOWNOMAL =1;

  privatestaticvoid HandleRunningInstance(Process instance)

  {

  ShowWindowAsync(instance.MainWindowHandle, SW_SHOWNOMAL);//显示

  SetForegroundWindow(instance.MainWindowHandle);//当到最前端

  }

  privatestatic Process RuningInstance()

  {

  Process currentProcess = Process.GetCurrentProcess();

  Process[] Processes = Process.GetProcessesByName(currentProcess.ProcessN ame);

  foreach (Process process in Processes)

  {

  if (process.Id != currentProcess.Id)

  {

  if (Assembly.GetExecutingAssembly().Location.Replace("/", "\\") == currentProcess.MainModule.FileName)

  {

  return process;

  }

  }

  }

  returnnull;

  }

  代码

  Process process = RuningInstance();

  if (process ==null)

  {

  Application.Run(new Form1());

  }

  else

  {

  MessageBox.Show("应用程序已经在运行中。。。");

  HandleRunningInstance(process);

  //System.Threading.Thread.Sleep(1000);

  //System.Environment.Exit(1);

  }

  这里整理了三种方案,希望朋友们提出更多的解决方案