C#软件启动设计

2016-01-29 13:10 23 1 收藏

C#软件启动设计,C#软件启动设计

【 tulaoshi.com - ASP.NET 】


本文目的:根据近期开发的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); } }/*以上代码就可以实现全部的软件启动功能,如果要填加登陆窗口,可以放在初始化窗口之后,也可以放在主窗口类来实现里。*/ }}

来源:https://www.tulaoshi.com/n/20160129/1489146.html

延伸阅读
Properties 在C#中为类预定义属性是件再简单不过的事,见程序1。 程序1 using System;namespace PropertiesDemo{ public class MyData { ............... } public class Class1 { private MyData _data; public MyData Data { get { return _data; } } public Class1() { _data = new MyD...
获取文件的版本信息: FileVersionInfo myFileVersionInfo1 = FileVersionInfo.GetVersionInfo("D:\\TEST.DLL"); textBox1.Text="版本号: " + myFileVersionInfo1.FileVersion; 更改文件属性,删除只读文件: 下例欲将E:\test.txt文件拷贝至D:\tmp\test.txt,但D:\tmp\test.txt已经存在。 //File.Copy(sourceFile,destinationFile,tr...
许多软件都有自动关机功能,特别是在长时间下载的时候,这个功能可是使你不用以守候在计算机前面,而电脑却能按照您事先的设定自动关闭。现在我们用visual C#来编写一个多功能的关机程序。该程序具有:定时关机、倒计时关机、关机提醒、系统信息获取等四项功能, 可设定关机时间精确到秒。并且让你很快掌握Visual C#中对API的操作程序。 一. ...
状态模式主要解决当控制一个对象状态的转换的条件表达过于复杂的情况,使得状态的转换不依赖于整体的操作。本文将通过一个具体的例子说明状态模式的应用。假设下面一个场景:      一个新任务提交后,先是收集数据,数据收集完成后等等分配一台机器,分配到机器后就可以将此任务部署至此机器后就可以通知相关模块开始工作...
What is a GUID For those of you who don''t know, a GUID (pronounced goo''id - Globally unique identifier) is a 128-bit integer that can be used to uniquely identify something. You may store users or products in your database and you want somehow uniquely identify each row in the database. A common approach is to crea...

经验教程

667

收藏

43
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部