用C#給程序加啟動畫面并只允許一個應用程序實例運行
2024-07-21 02:18:44
供稿:網友
涉及類:
1、 啟動畫面類:
public class splashform : system.windows.forms.form
{
private system.windows.forms.picturebox picturebox1;
private system.windows.forms.label label1;
private system.windows.forms.label lbl_version;
/// <summary>
/// 必需的設計器變量。
/// </summary>
private system.componentmodel.container components = null;
public splashform()
{
//
// windows 窗體設計器支持所必需的
//
initializecomponent();
lbl_version.text = "版本:" + application.productversion;
//
// todo: 在 initializecomponent 調用后添加任何構造函數代碼
//
}
//以下省略
2、 應用程序加載類:
using system;
using system.drawing;
using system.collections;
using system.componentmodel;
using system.windows.forms;
using system.data;
using system.runtime.interopservices;
using system.diagnostics;
using system.reflection;
using system.io;
namespace heroic.tempanalyse.tempgui
{
/// <summary>
/// apploader 的摘要說明。
/// </summary>
public class apploader
{
private static applicationcontext context;
private static splashform sform = new splashform();
private static mainwindow mform = null;
//0不可見但仍然運行,1居中,2最小化,3最大化
private const int ws_shownormal = 3;
[stathread]
static void main(string[] args)
{
// [8/12/2004]用于更新該程序。
doupdata();
// [7/19/2004] 改變順序,目的使得開始加載速度加快
//得到正在運行的例程
process instance = runninginstance();
if(instance == null)
{
sform.show();
mform = new mainwindow();
context = new applicationcontext();
application.idle += new eventhandler(onappidle);
application.run(context);
}
else
{
//處理發現的例程
handlerunninginstance(instance);
//messagebox.show("當前程序已經運行了!");
}
}
//在線更新用,不再本文范圍
private static void doupdata()
{
system.diagnostics.process.start([email protected]"/update.exe",[email protected]"/heroic.tempanalyse.tempgui.exe 0");//
}
private static void onappidle(object sender, eventargs e)
{
if(context.mainform == null)
{
application.idle -= new eventhandler(onappidle);
mform.preload();
context.mainform = mform;
context.mainform.show();
sform.close();
sform = null;
}
}
//不允許有兩個程序同時啟動
public static process runninginstance()
{
process current = process.getcurrentprocess();
process[] processes = process.getprocessesbyname (current.processname);
//遍歷正在有相同名字運行的例程
foreach (process process in processes)
{
//忽略現有的例程
if (process.id != current.id)
{
//確保例程從exe文件運行
if (assembly.getexecutingassembly().location.replace("/", "//") ==
current.mainmodule.filename)
{
//返回另一個例程實例
return process;
}
}
}
//沒有其它的例程,返回null
return null;
}
public static void handlerunninginstance(process instance)
{
//確保窗口沒有被最小化或最大化
showwindowasync (instance.mainwindowhandle , ws_shownormal);
//設置真實例程為foreground window
setforegroundwindow (instance.mainwindowhandle);
}
[dllimport("user32.dll")]
private static extern bool showwindowasync(
intptr hwnd, int cmdshow);
[dllimport("user32.dll")] private static extern bool
setforegroundwindow(intptr hwnd);
}
}
3、 加載完畢正式運行后的類:
public void preload()
{
// 如果已經加載畢,則返回
if (_loaded)
return;
// 把機器生成的代碼放到這里
initcustomcontrol();
_loaded = true;
}
// 是否加載完畢
private bool _loaded = false;
protected override void onload(eventargs e)
{
// 確保 preload()函數已經調用
if (!_loaded)
throw new invalidoperationexception("must call preload before calling this function.");
}