C# 关于程序退出问题学习

572 阅读3分钟

持续创作,加速成长!这是我参与「掘金日新计划 · 10 月更文挑战」的第23天,点击查看活动详情

前言:

C#程序退出有几种方法,但是具体的方法是什么呢,我们会用吗,当然也有可能会用的,也有不会用,对于这方面的知识,我们都得去学习一下,去探讨一下,对于我们开发程序或者是去学习,我们都得去敲一下看看,自己会不会,不会就学,创作不易,点赞关注评论收藏,你的点赞是我创作的动力,也是我学习的方向。 d97acf8bd6adc01a9bf0ed9bab50b0c2.gif

界面设计:

我们新建一个程序,简单设计一下窗口,并多建几个窗口,方便我们去关闭

image.png

Close方法

Close方法是关闭窗口,如果你是在主窗口调用其他的窗口,想在主窗口关闭其他串口,Close是不能实现的,Close只能关闭当前活动界面的窗体。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp8
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Form2 form2 = new Form2();
            form2.Show();
        }
    }
}

image.png

image.png

Application.Exit(); 方法

强制所有消息中止,退出所有的窗体,但是若有托管线程(非主线程),也无法干净地退出;

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp8
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
             Application.Exit();
        }
    }
}


image.png

Application.ExitThread(); 

强制中止调用线程上的所有消息,同样面临其它线程无法正确退出的问题,这个函数和上一个差不多,这个是退出线程,在其他窗口调用会退出主线程

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp8
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
           Application.ExitThread(); 
        }
    }
}

image.png

System.Environment.Exit(0); 

这是最彻底的退出方式,不管什么线程都被强制退出,把程序结束的很干净,退出时有可能会抛出异常

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp8
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
           System.Environment.Exit(0);
        }
    }
}

image.png

System.Diagnostics.Process的Kill

直接杀死与本程序相关的所有进程,有可能会导致数据丢失,但是不会抛出异常。

image.png

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp8
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process process = System.Diagnostics.Process.GetProcessById(System.Diagnostics.Process.GetCurrentProcess().Id);
            process.Kill();
        }
    }
}

总结

这篇文章比较简单,只是简单的学习一下,对它有更多的认识,在有需求的时候最起码有路子,虽然很简单,但是也是可以学到东西的,我们学习了新的知识,对我们的知识储备及技术又有新的一点点的进步,C#的技术就是先简单再难嘛,积少成多之后才会成长才会进步,我们要不断的学习不断的探索,才能有学习的动力,才会有学习的欲望,创作不易,点赞评论收藏关注,嘿嘿,不喜勿喷!!!!

202104151629418126.gif