线程未关闭或终止 [英] Thread not closing or terminating

查看:84
本文介绍了线程未关闭或终止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个多线程简单控制台程序,我想在按字符"t"时启动一个线程,并在按字符"x"时关闭它.以下是代码,但是当我第一次按"t"键时,它也开始按"x"键关闭,但是当我第二次尝试按"t"键时,它卡住了.

This is a multi thread simple console program , i want to start one thread on pressing the character "t" and close it on pressing the character "x". the following is the code but when i press "t" for the first time its started and close on pressing "x" as well but when i tried to press "t" for the second time its stuck.

<pre><br />
using System;<br />
using System.Threading;<br />
namespace consoleApp2<br />
{<br />
    class ThreadTest<br />
    {<br />
        static void Main()<br />
        {<br />
            Thread t1 = new Thread(staric); //  this is staric in a positin e<br />
            Thread t2 = new Thread(staric2); // this is staric 2 in diffrent position<br />
            while (true)<br />
            {<br />
                char x = Console.ReadKey().KeyChar;<br />
                int Time = 1;<br />
                if (x == ''t'' && Time == 1)<br />
                {<br />
                    Time++;<br />
                    t1.Start();<br />
                }<br />
                else if (x == ''t'' && Time > 1)<br />
                {<br />
                    Time++;<br />
                    t2.Start();<br />
                }<br />
                if (x == ''x'' && Time == 1)<br />
                {<br />
                    Time++;<br />
                    t1.Suspend();<br />
                }<br />
                else if (x == ''x'' && Time == 2)<br />
                {<br />
                    Time++;<br />
                    t2.Suspend();<br />
                }<br />
            }<br />
        }<br />
        static void staric()<br />
        {<br />
            while (true)<br />
            {<br />
                Console.SetWindowPosition(0, 0);<br />
                Console.Write("-");<br />
                Thread.Sleep(70);<br />
                Console.Clear();<br />
                Console.Write("\\");<br />
                Thread.Sleep(70);<br />
                Console.Clear();<br />
                Console.Write("|");<br />
                Thread.Sleep(70);<br />
                Console.Clear();<br />
                Console.Write("/");<br />
                Thread.Sleep(70);<br />
                Console.Clear();<br />
            }<br />
        }<br />
        static void staric2()<br />
        {<br />
            while (true)<br />
            {<br />
                Console.SetWindowPosition(0, 1);<br />
                Console.Write("s");<br />
            }<br />
        }<br />
    }<br />
}</pre><br />

推荐答案

使用t1.Join,t2.Join而不是Suspend.

当您告诉Join时,它将阻塞所有操作,直到线程完成.

祝您好运
Use t1.Join, t2.Join rather then Suspend.

When you you tell Join, it blocks everything until the thread is done.

Good luck


您的逻辑错误(每个while周期将Time设置为1).
:)
Your logic is wrong (Time is set to 1 at every while cycle).
:)


这篇关于线程未关闭或终止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆