WinForm(二):WinFrom中Main函数的入参和出参

基本上有独立进程的应用,都是以Main函数作为入口,开始运行的。在C#中,Main函数可以无参无返回值,当然也可以是有string[]参数和int返返回值的。WinFrom也满足这个规则。.

那么Main作为一个进程的开始函数,那么是谁传这些参数呢?然后又是谁来接收这个返回值呢?显然不可能是进程本身,那么就是系统,或其他进程了。

namespace WinFormDemo01{    internal static class Program    {        [STAThread]        static int Main(string[] args)        {            MessageBox.Show($"Main方法中收到的参数:{string.Join(',', args)}");            ApplicationConfiguration.Initialize();            Application.Run(new Form1());            return 101;        }          }}

.NET中,除了在Main方法内接收参数,还可以用Enviroment.CommandLine属性和GetCommandLineArgs方法来接收,属性会用空格来区分多个参数,方法返回值就是一个string[],与Main中args不一样的是,这里多一个应用运行所在的路径。下面的代码是在Form1窗体里,用一个按钮来显示接收到的Main方法参数。

namespace WinFormDemo01{    public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();        }
        private void button1_Click(object sender, EventArgs e)        {           //MessageBox.Show(Environment.CommandLine);            var args = Environment.GetCommandLineArgs();            if (args.Length > 1)            {                label1.Text = $"主程序:{args[0]},\r\n\r\n参数:{string.Join(',', args[1..])}";            }          }         }}

 

用批处理来调用进程

我们看一下用在widnows系统中来调用bat,实现对Main函数的送参和取返回值,比如这个bat名称为result.bat,要求这个文件与我们程序在一个文件夹下,启一个命令行程序,进入到该目录,运行rusult.bat即可。.

@echo off  WinFormDemo01 guisuwei 1234567890@if "%ERRORLEVEL%" == "0" goto ok    :fail      echo Execution Failed      echo return value = %ERRORLEVEL%      goto end    :ok      echo Execution succeeded      echo Return value = %ERRORLEVEL%      goto end    :end

结果如下,可以看到Main中的弹框参数

WinForm(二):WinFrom中Main函数的入参和出参

Form1中取Main函数参数

WinForm(二):WinFrom中Main函数的入参和出参

在命令行中接收到的返回值是101

WinForm(二):WinFrom中Main函数的入参和出参

用一个进程来调用这个进程

用批处理来调用的情况虽然可实现,但使用场景还有有限的,一般情况下我们是用一个进程调用另一个进程,就像一个是主程序,其他都是子程序类似。

我们用WinFormDemo00来调用WinFormDemo01,这里使用了两种方式,阻塞方式,也叫同步方式,另一种是非阻塞方式,也可以叫异步方式。buton1_click方法里是异步,就是调起WinFormDemo01后,WinFormDemo00该做什么做什么,不会卡住等它的返回值。button2_click就是这种情况,会卡住,直到返回结果。

using System.Diagnostics;
namespace WinFormDemo00{    public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();        }               private void button1_Click(object sender, EventArgs e)        {            //WinFormDemo01所在路径            var pro = Process.Start(@"C:\MyFile\Asp.NetCoreExperiment\Asp.NetCoreExperiment\WinForm\WinFormDemo01\bin\Debug\net7.0-windows\WinFormDemo01.exe", new string[] { "gsw", "abcd" });             //必须启用这个属性,事件才能生效            pro.EnableRaisingEvents = true;            pro.Exited += Pro_Exited;        }
        private void Pro_Exited(object? sender, EventArgs e)        {           //由于是异常,这个方法所在的线程与UI线程不是同一个,所以用这种方式来操作UI线程的控件            this.Invoke((object sender) =>            {                label1.Text = $"{DateTime.Now},WinFormDemo01返回值:{(sender as Process)?.ExitCode.ToString()}";            }, sender);         }
        private void button2_Click(object sender, EventArgs e)        {            var pro = Process.Start(@"C:\MyFile\Asp.NetCoreExperiment\Asp.NetCoreExperiment\WinForm\WinFormDemo01\bin\Debug\net7.0-windows\WinFormDemo01.exe", new string[] { "gsw", "abcd" });            //阻塞                pro.WaitForExit();               label1.Text = $"{DateTime.Now},WinFormDemo01返回值:{pro.ExitCode.ToString()}";        }    }}

WinFormDemo00界面布局。

WinForm(二):WinFrom中Main函数的入参和出参

其实大部分桌面应用都是单体,所以Main函数的参数和返回值无所谓,但如果你们是做系列产品的,有很多个应用,每个应用都需要登录,那么,可以通过这种方法把一个应用中的用户信息或其他信息传送到另一个应用中,不需要重新登录,当然,前提是两个系统共享用户系统数据。另外,涉密数据的传送要注意加密。