当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > C#软件启动设计

ASP.NET
ASP.NET 多附件上传实现代码
asp.net 类库中使用ConfigurationManager.ConnectionStrings
asp.net AjaxControlToolKit--TabContainer控件的介绍
ASP.NET环境下为网站增加IP过滤功能
asp.net访问Access数据库溢出错误
ASP.NET AJAX 4.0的模版编程(Template Programming)介绍
asp.net StringBuilder的用法 实例代码
asp.net Accee数据库连接不稳定解决方案
ASP.NET2.0 SQL Server数据库连接详解
asp.net Repeater之非常好的数据分页
C# 生转换网页为pdf
浅谈ASP.NET的Postback 实例代码
asp.net Ext grid 显示列表
通过ASP.net实现flash对数据库的访问
如何创建一个AJAXControlToolKit的扩展控件
asp.net 简单验证码验证实现代码
asp.net 在客户端显示服务器端任务处理进度条的探讨
Asp.net treeview实现无限级树实现代码
asp.net INI文件读写类
asp.net下检测SQL注入式攻击代码

ASP.NET 中的 C#软件启动设计


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


本文目的:根据近期开发的C#软件,对于软件的启动设计谈谈我的心得。
如下代码是我设计的启动软件的类,应用程序入口也是在这个普通类里面。
using System;
using System.Threading;
using System.Windows.Forms;
namespace MainClass
{
public class MainApp
{
private static Mutex myMutex;
private static bool requestInitialOwnership = true;
private static bool mutexWasCreated;
/*
以上的部分是声明进程的互斥
*/
[STAThread]
static void Main()
{
try
{
myMutex = new Mutex(requestInitialOwnership,"Test",out mutexWasCreated);
if(!(requestInitialOwnership && mutexWasCreated))
myMutex.WaitOne();
else
new MainApp();
/*
这里就是进程互斥的实现。我看过一些人写的启功互斥,他们采用的方式是先看当前进程表里有没有要启动的进程;有,看看这个进程是否和要运行的进程来之相同的目录。
实际上看来,这样不能彻底解决问题,例如,如果我把程序改名,软后换个目录这样就可以在此运行了,而且时间复杂度偏大。
而以上的代码:
myMutex = new Mutex(requestInitialOwnership,"Test",out mutexWasCreated);

这里是申请一个命名互斥,并且返回是否已经有同名的申请了。
if(!(requestInitialOwnership && mutexWasCreated))

myMutex.WaitOne();

如果互斥已经申请过了,阻塞要运行的程序。
*/
}
}
catch(Exception ed)
{
MessageBox.Show(ed.ToString(),"Wrong Convention",MessageBoxButtons.OK,MessageBoxIcon.Error);
System.Environment.Exit(0);
}
}
public MainApp()
{
/*
以下是启动类
*/
try
{
//这个窗口就是初始化窗口,也可以说是软件封皮
MainClass.InitializeForm.InitializeForm initializeForm = new MainClass.InitializeForm.InitializeForm();

//在初始化窗口里面添加检验程序,一般是用来初始化数据库
string result;
if((result = initializeForm.StartTest()) != "")
throw new Exception(result);
else
initializeForm.Close();
//结束初始化窗口,最后进入主窗口
Application.Run(new MainForm.MainForm());
}
catch(Exception ed) { MessageBox.Show(ed.ToString(),"#error#",MessageBoxButtons.OK,MessageBoxIcon.Error); Environment.Exit(1); } }/*以上代码就可以实现全部的软件启动功能,如果要填加登陆窗口,可以放在初始化窗口之后,也可以放在主窗口类来实现里。*/ }}