当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > 用C#给程序加启动画面并只允许一个应用程序实例运行

ASP.NET
ASP.NET超凡的代码控制(二)
剖析ASP.NET下部构造(一)
剖析ASP.NET下部构造(二)
ASP.NET的Web controls(一)
ASP.NET的Web controls(二)
如何在页面上动态的生成 WebForm控件
再议正则表达式(这次是在asp.net 上的应用)
利用.NET框架简化发布和解决DLL Hell问题(1)
利用.NET框架简化发布和解决DLL Hell问题(2)
在ASP+中访问数据库
通过事例学习.net的WebForms技术(一)
通过事例学习.net的WebForms技术(二)
如何在aspx中得到在存储过程中的的数值(兼答jspfuns的问题)
如何得到一个汉字和字母组合的字符串的准确的长度(asp.net 版本的)
ASP+中取代ASP的RS(Remote Scripting)技术的Framework
通过事例学习.net的WebForms技术(三)
通过事例学习.net的WebForms技术(四)
通过事例学习.net的WebForms技术(五)
通过网络域名得到这台主机的IP地址
查看服务器磁盘、文件的aspx

ASP.NET 中的 用C#给程序加启动画面并只允许一个应用程序实例运行


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-11-03   浏览: 92 ::
收藏到网摘: n/a

涉及类: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; /// /// 必需的设计器变量。 /// 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 { /// /// AppLoader 的摘要说明。 /// 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(Application.StartupPath+@"\update.exe",Application.StartupPath+@"\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."); }