JLWLjlwl 2021-05-19 22:55 采纳率: 76.9%
浏览 43
已采纳

C# 之前在提问区请教的一个问题,自己变化时发现行不通,遂请问各位老师

  
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            //线程清理
            t.Abort();
            if (t1 != null) t1.Abort();
        }
        System.Threading.Thread t = null, t1 = null;
        private void Form1_Load(object sender, EventArgs e)
        {
            this.t=new System.Threading.Thread(() =>
            {
                while (true)
                {
                    var num = System.Diagnostics.Process.GetProcesses().Where(i => i.ProcessName == "notepad").Count();
                    if (num > 0)
                    {//程序被开启,启动新线,3s后执行
                        t1 = new System.Threading.Thread(() => {
                            System.Threading.Thread.Sleep(3000);//先停3s,在执行操作
                            
                            //其他操作
                            MessageBox.Show("记事本程序已启动!");
                            //Form1 frm=new Form1();
                            //frm.Show();
                            t.Abort();
                        });
                        t1.Start();
                    }
                    else if (t1 != null) { t1.Abort(); t1 = null; }
                    System.Threading.Thread.Sleep(3000);//3s检查一次
                }
            });
            this.t.Start(); 
        }

请问在这段代码中,执行其他操作的语句里,用何种语句可以在执行完“MessageBox.Show("记事本程序未启动!");Form1 frm=new Form1();frm.Show();”之后,使循环停止并隐藏这个窗口。学习C#时间不太长,使用break等都没起作用或不能在线程中执行,请问各位老师知道如何能实现这个操作的代码吗,希望有老师指教,谢谢老师

原问题:https://ask.csdn.net/questions/7428950  感谢GoCityPass新加坡曼谷通票老师的回答

  • 写回答

1条回答 默认 最新

  • Go 旅城通票 2021-05-20 09:26
    关注
     private void Form1_FormClosing(object sender, FormClosingEventArgs e)
            {
                //线程清理
                if (t.IsAlive) t.Abort();
                if (t1 != null && t1.IsAlive) t1.Abort();
            }
            System.Threading.Thread t = null, t1 = null;
            delegate void showForm();
            private void Form1_Load(object sender, EventArgs e)
            {
                this.t = new System.Threading.Thread(() =>
                {
                    while (true)
                    {
                        var num = System.Diagnostics.Process.GetProcesses().Where(i => i.ProcessName == "notepad").Count();
                        if (num == 0 && (t1 == null || !t1.IsAlive))
                        {//未启动对应程序,并且t1未初始化或者未运行状态
                            Console.WriteLine(t1 == null);
    
                            t1 = new System.Threading.Thread(() =>
                            {
                                System.Threading.Thread.Sleep(3000);//先停3s,在执行操作
                                t.Abort();//要放到MessageBox.Show前面,后面好像无法停止线程。。
                                MessageBox.Show("记事本程序未启动!");
                                if (this.InvokeRequired)//要在线程中打开其他窗体,需要Invoke过,更新窗体控件也需要Invoke
                                    this.Invoke(new showForm(() =>
                                {
                                    var frm = new Form2();
                                    frm.Show();
                                }));
    
                            });
                            t1.Start();
                        }
                        else if (t1 != null && t1.IsAlive) { t1.Abort(); t1 = null; }
    
                        System.Threading.Thread.Sleep(3000);//3s检查一次
                    }
                });
                this.t.Start();
            }
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多