熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> ASP編程 >> 正文

C#中讓程序只運行一個實例的操作方法

2022-06-13   來源: ASP編程 

  讓程序只運行一個實例比如啟動殺毒軟件時只能啟動一個再啟動的話就沒什麼效果!利用程序名來判斷不是一個好辦法如果我們把程序名稱改一下就可以運行兩個完全一樣的進程我們最好利用程序集的Attribute存放特定信息然後用Assembly對象的GetCustomAttributes()方法獲取該信息進行判斷

  在這裡我有兩個解決方法

  方法一我把AssemblyInfocs裡的[assembly AssemblyFileVersion()]改為[assemblyAssemblyFileVersion()]然後利用該信息進行判斷

  代碼如下

  using System;

  using SystemCollectionsGeneric;

  using SystemLinq;

  using SystemText;

  using SystemDiagnostics;

  using SystemReflection;

  using SystemCollections;

  using SystemThreading;

  namespace MyWork_

  {

  class Program

  {

  static void Main(string[] args)

  {

  Process[] processes = ProcessGetProcesses(); //獲得當前所有進程

  Process currentProcess = ProcessGetCurrentProcess(); //獲取當前正在運行進程

  ProcessModule currentPM = currentProcessModules[];

  int same = ; //相同運行實例個數

  ArrayList proList = new ArrayList(); //將相同實例加入此集合中

  foreach (Process p in processes)

  {

  try//由於進程不同有的進程不包含Modules信息所以要用try保護

  {

  if (pModules != null)

  if (pModulesCount > )

  {

  SystemDiagnosticsProcessModule pm = pModules[];

  if (pmFileVersionInfoFileVersionEquals(currentPMFileVersionInfoFileVersion))

  {

  same++;

  proListAdd(p);

  }

  if (same > )

  {

  same++;

  proListAdd(p);

  if (same > )

  {

  for (int i = ; i < proListCount; i++)

  if (((Process)(proList[i]))Id == currentProcessId)

  {

  ConsoleWriteLine(該進程已經啟動了一個實例);

  ThreadSleep();

  ((Process)(proList[i]))Kill();

  }

  }

  }

  }

  }

  }

  catch

  { }

  }

  ConsoleRead();

  }

  }

  }

  方法二直接定義一個屬性類利用此屬性信息進行判斷
代碼如下
?using System;

  using SystemCollectionsGeneric;

  using SystemLinq;

  using SystemText;

  using SystemReflection;

  using SystemDiagnostics;

  using SystemCollections;

  using SystemThreading;

  [assembly: Help(This Assembly demonstrates custom attributes creation and their runtime query)]

  public class HelpAttribute : Attribute
{
public HelpAttribute(String Description_in)
{
thisdescription = Description_in;
}

  protected String description;

  public String Description
{
get
{
return thisdescription;
}
}
}
class Program
{
static void Main(string[] args)
{
HelpAttribute HelpAttr = null;
HelpAttribute HelpAttr = null;
Process currentProcess = ProcessGetCurrentProcess(); //獲取當前正在運行進程
Assembly a = AssemblyLoadFrom(currentProcessMainModuleFileName);
foreach (Attribute attr in aGetCustomAttributes(true))
{
HelpAttr = attr as HelpAttribute;
if (null != HelpAttr)
{
//ConsoleWriteLine(Description of {}:\n{} currentProcessMainModuleFileName HelpAttrDescription);
break;
}
}
Process[] processes = ProcessGetProcesses(); //獲得當前所有進程
int same = ; //相同運行實例個數
ArrayList proList = new ArrayList(); //將相同實例加入此集合中
foreach (Process pro in processes)
{
try//由於進程不同有的進程不包含Modules信息所以要用try保護
{
if (proModules != null)
if (proModulesCount > )
{
Assembly b = AssemblyLoadFrom(proMainModuleFileName);
foreach (Attribute attr in bGetCustomAttributes(true))
{
HelpAttr = attr as HelpAttribute;
if (null != HelpAttr)
{
if (HelpAttrDescriptionEquals(HelpAttrDescription))
{
same++;
proListAdd(pro);
if (same > )
{
for (int i = ; i < proListCount; i++)
{
if (((Process)(proList[i]))Id == currentProcessId )
{
ConsoleWriteLine(該進程已經啟動了一個實例);

  ThreadSleep();

  ((Process)(proList[i]))Kill();

  }
}
}
}
}
}
}
}
catch
{
}
}
ConsoleReadLine();
}


From:http://tw.wingwit.com/Article/program/ASP/201311/21798.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.