首页 新闻 会员 周边

超线程问题!!!

0
悬赏园豆:5 [已解决问题] 解决于 2016-10-13 23:54
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Threading;
namespace WgKey
{
    public partial class XCTest : Form
    {
        //通知一个或多个正在等待的线程已发生事件。false默认不启动 默认true启动
        static ManualResetEvent _mre = new ManualResetEvent(false);
        private Thread t;
        public XCTest()
        {
            InitializeComponent();
        }
        private void XCTest_Load(object sender, EventArgs e)
        {
            //创建超线程
            t = new Thread(new ThreadStart(this.GoBut));
        }


        private void button1_Click(object sender, EventArgs e)
        {
            //开启线程
            t.Start();
        }
        /// <summary>
        /// 线程启用
        /// </summary>
        private void KSXC()
        {
            _mre.Set();
        }

        /// <summary>
        /// 暂停线程
        /// </summary>
        private void ZTXC()
        {
            _mre.Reset();
        }
        private void GoBut()
        {
            while (true)
            {
                //阻止当前线程,直到收到信号。
                _mre.WaitOne();

                MessageBox.Show("1"); 
                System.Threading.Thread.Sleep(100);//暂停0.1秒
                MessageBox.Show("2");
            }
            //线程开始 t.Start();
            //线程暂停  t.Suspend();
            //线程恢复 t.Resume();
            //线程结束  t.Abort();
        }
    }
}

现在遇到了2个问题。

第一个问题:

GoBut()方法里的MessageBox.Show("2");不会执行。
第二个问题:
比如我的项目生成了test.exe,运行了这个程序以后,把这个程序关闭掉,但是进程test.exe不会消失一直存在。。
87Super的主页 87Super | 初学一级 | 园豆:107
提问于:2016-05-03 15:55
< >
分享
最佳答案
0
  1. 你把暂停时间设置的长一点,使第一个MessageBox点完确定后暂停还没结束
  2. 在窗口关闭事件中结束test.exe
收获园豆:3
刘宏玺 | 专家六级 |园豆:14020 | 2016-05-03 16:02

能具体一点吗?怎么结束text.exe?

this.Close();么?

87Super | 园豆:107 (初学一级) | 2016-05-03 16:14

@87Super: 可以使用Process类,请自行百度

刘宏玺 | 园豆:14020 (专家六级) | 2016-05-03 16:20

@刘宏玺: 我看了,但是我感觉不需要用Process。应该是代码上有不完全的地方。

87Super | 园豆:107 (初学一级) | 2016-05-03 16:24

@87Super: 那你试试在关闭的事件里面加入这句话

System.Environment.Exit(0);

刘宏玺 | 园豆:14020 (专家六级) | 2016-05-03 16:25

@刘宏玺: 找到解决办法了。

//设置为后台进程,这样当主线程退出时,这个线程就会退出
            t.IsBackground = true;
87Super | 园豆:107 (初学一级) | 2016-05-03 17:07
其他回答(2)
0

你的问题如何产生,你KSXC和ZTXC又如何使用

收获园豆:1
Yu | 园豆:12980 (专家六级) | 2016-05-03 17:02
0

在窗体的Close事件里面加一句:            t.Abort();

因为你的线程还挂在那里,当然不会退出了。你需要终结掉它

收获园豆:1
需要格局 | 园豆:2145 (老鸟四级) | 2016-05-03 17:14
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册