您的位置:首页 > 其它

应用程序返回值的设置与获取

2010-07-29 16:40 99 查看
program 1 Code:

private void button1_Click(object sender, EventArgs e)
{
Process process = new Process();
process.StartInfo.FileName =Application.StartupPath +"//parameters.exe";
process.StartInfo.Arguments = this.textBox1.Text.Trim();
int isEx = -1;

process.Start();
process.WaitForExit();
isEx = process.ExitCode;
Trace.WriteLine("retruen: " + isEx);
textBox2.Text = isEx.ToString();
}


program 2 Code:

ProjectName:parameters.exe

static int Main(string[] args)
{
if (args.Length > 0)
{
for (int i = 0; i < args.Length; i++)
{
Console.WriteLine("param: " + args[i].ToString());
}
Console.ReadLine();
return 10;

}
else
{
Console.ReadLine();
return 5;
}
}


下面的代码反映一种巧妙的返回值机制:

static int Main(string[] args)
{
try
{

//---------------------------------------------------------------------
bool onlyInstance = false;
// if you want your app to be limited to a single instance across ALL SESSIONS (multiple users & terminal services), then use the following line instead:
string mutexName = String.Format("Global//{0}.{1}", applicationName, AssemblyGuidString);
Mutex mutex = new Mutex(true, mutexName, out onlyInstance);
if (!onlyInstance)
{
//For FindWindow to work, applicationName must be the same as the title (the Text property) of your MainForm.
IntPtr hwndFirstInstance = WinHelper.FindWindow(null, applicationName);
WinHelper.PostMessage(hwndFirstInstance, WinHelper.WM_SHOWFIRSTINSTANCE, IntPtr.Zero, IntPtr.Zero);

return CommConst.MAINRETURN_USERALREADYLOGIN;
}

//-------------------------------------------------------------------
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
log4net.Config.XmlConfigurator.Configure();

//-------------------------------------------------------------------
//cross the windows control,login failed, return msg
if (args != null
&& args.Length == 4 && ??? && ???)
{

LoginForm loginForm = new LoginForm(args[1], args[3]);
int rtn = loginForm.MainReturnMsg;
if (rtn == CommConst.MAINRETURN_NAMEORPWDERROR
|| rtn == CommConst.MAINRETURN_NETEXCEPTION
|| rtn == CommConst.MAINRETURN_USERLOCKUP
|| rtn == CommConst.MAINRETURN_USERALREADYLOGIN)
{
return rtn;
}
}

//---------------------------↓---------chenys
//如果要通过代码运行进行调试,这里的几行代码请暂时注释掉,不然没有办法进行跟踪调试
if (args != null && args.Length == 0)
{
Process process = new Process();
process.StartInfo.FileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "XXXClient.Update.exe");
process.Start();

return CommConst.MAINRETURN_UPDATING;//
}
//---------------------------↑---------

//------------------------------------------------------------------

if (args != null && args.Length == 0)
{
Application.Run(new XXXClientAppContext());
}
else if (args != null && args.Length == 1)
{
Process[] processList = Process.GetProcessesByName("XXXClient.Update");
for (int i = 0; i < processList.Length; i++)
{
processList[i].Kill();
}

Application.Run(new XXXClientAppContext());
}
else if (args != null
&& args.Length == 4
&& ???
&& ???)
{
//登录验证成功:1
Application.Run(new XXXClientAppContext(1));
}

//
GC.KeepAlive(mutex);
return CommConst.MAINRETURN_OTHER;

}
catch (Exception ex)
{
throw ex;
}
}


总结,为了忘却。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐