当前位置:网站首页>WinForm(二):WinFrom中Main函数的入参和出参
WinForm(二):WinFrom中Main函数的入参和出参
2022-08-03 03:33:00 【dotNET跨平台】
基本上有独立进程的应用,都是以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中的弹框参数
Form1中取Main函数参数
在命令行中接收到的返回值是101
用一个进程来调用这个进程
用批处理来调用的情况虽然可实现,但使用场景还有有限的,一般情况下我们是用一个进程调用另一个进程,就像一个是主程序,其他都是子程序类似。
我们用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界面布局。
其实大部分桌面应用都是单体,所以Main函数的参数和返回值无所谓,但如果你们是做系列产品的,有很多个应用,每个应用都需要登录,那么,可以通过这种方法把一个应用中的用户信息或其他信息传送到另一个应用中,不需要重新登录,当然,前提是两个系统共享用户系统数据。另外,涉密数据的传送要注意加密。
边栏推荐
- ClickHouse—入门
- How to write test cases in software testing technology (2)
- 详细讲解一下JVM的内存模型与实现?
- log4j设置日志的时区
- 我的“眼睛”就是尺!
- 2022-08-02 顾宇佳 学习笔记 多线程
- 视频中场的概念(1080I和1080P)和BT601/656/709/1120/2020/2077
- conda常用命令合集
- Chapter 8 Character Input Output and Input Validation
- Have bosses know date field flinksql is synchronized to the use of the null on how to deal with
猜你喜欢
随机推荐
stdio.h(本机代码)
Jincang Database Pro*C Migration Guide (3. KingbaseES Pr*oc Compatibility with Oracle Pro*c)
ClickHouse—高级
什么是数据标注? 数据标注公司主要做什么?
conda常用命令合集
OneNote 教程,如何在 OneNote 中设置笔记格式?
黑马程序员Servlet
工业边缘计算研究现状与展望
成都高新南区 高新西区 东部新区 多边形范围点位 AOI 高德
金仓数据库 Pro*C 迁移指南( 4. KingbaseES 的 Pro*C 迁移指南)
Scala基础【异常、隐式转换、泛型】
爆肝22个ES6知识点
智能健身动作识别:PP-TinyPose打造AI虚拟健身教练!
Guys, I don't understand a bit: why the documentation of oracle-cdc writes that the connector can be done exactly-o
js的组成及js样式
软件测试技术之如何编写测试用例(2)
voliate关键字
(一)Nacos注册中心集群环境搭建
2022-08-01 顾宇佳 学习笔记
【STM32】入门(三):按键使用-GPIO端口输出控制