微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – 线程无法按预期启动

我正在尝试进行测试,看看是否有某些技能.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication2
{
    class timerup
    {
        public bool timeup = false;
    }

    class Program
    {
        public static void timer()
        {
            for (int i = 1; i < 3; i++)
            {
                System.Threading.Thread.Sleep(1000);
                if (i == 5)
                {
                    object a;
                    a = true;
                    a = new timerup();
                    timerup ClassRef;
                    ClassRef = (timerup)a;
                    ClassRef.timeup = true;
                }
            }
        }

        static void Main(string[] args)
        {
            Console.Title = "The Secret Agent Test";
            Console.BackgroundColor = ConsoleColor.Black;
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("Welcome,agent. This is the test to see if\nyou are good enough to be a full member of the OT Secret Agency.");
            Console.WriteLine("Do you want to continue? [Y/N]");
            string cont = Console.ReadLine();
            if (cont == "y" || cont =="Y")
            {
                Console.Clear();
                Console.WriteLine("Let's continue the test.");
                Console.WriteLine("Crack the password:");
                Console.WriteLine("Username: IdioT_NOOB1337\nPROFILE: Likes memes such as doge.\nIs an elitist (Over the things he likes)\nOnly uses the word idiot as an insult");
                Console.WriteLine("Password:");
                string pass1 = Console.ReadLine();
                if (pass1 == "AnyoneWhodoesn'tLikeDogeIsAnIdiot" || pass1 == "anyonewhodoesn'tlikedogeisanidiot")
                {
                    Console.WriteLine("Account accessed.");
                    Console.WriteLine("Stage 1 Complete.");
                    Console.WriteLine("Loading next level...");
                    System.Threading.Thread.Sleep(2000);
                    Console.WriteLine("Level 2 loaded.");
                    System.Threading.Thread.Sleep(1000);
                    Console.Clear();
                    Console.WriteLine("Nice. You certainly have skill. But this test.... determines speed of mind.");
                    System.Threading.Thread.Sleep(2500);
                    Console.Clear();
                    Console.WriteLine("You only have two seconds to answer the next question. Press any key when ready.");
                    Console.ReadKey();
                    Console.Clear();
                    Console.WriteLine("What is 12x12?!"); // QUESTION
                    System.Threading.Thread t = new System.Threading.Thread(new System.Threading.ThreadStart(timer)); // SUCH COMPLEX CODE FOR A TIMER... WTF.
                    string product = Console.ReadLine();
                    object b;
                    b = true;
                    b = new timerup();
                    timerup ClassRef;
                    ClassRef = (timerup)b;
                    bool timerthing = ClassRef.timeup;
                    if (product != "144" || timerthing == true)
                    {
                        Console.WriteLine("Sorry,you are incorrect. Restart the test again.");
                        System.Threading.Thread.Sleep(2000);
                        Console.Clear();
                        System.Environment.Exit(-1);
                    }
                    else
                    {
                        Console.WriteLine("Impressive. Your mind is fast,too. Well,be prepared for the next test. Pressure.");
                    }
                }
            }
        }
    }
}

线程不执行;我怀疑是因为字符串product = Console.ReadLine();位.这个测验的第二个问题是12×12,你有2秒的时间来回答,除了计算两秒钟的线程没有被执行…为什么……?如果你知道,我该如何解决

解决方法

一些重构代码解决问题的方法

using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading;
    using System.Diagnostics;

namespace ConsoleApplication2
{
    class Program
    {
        static void WriteText(params string[] lines) { WriteText(0,lines); }

        static void WriteText(double delaySecs,params string[] lines)
        {

            for (int i = 0; i < lines.Length; i++) Console.WriteLine(lines[i]);
            if (delaySecs > 0) Thread.Sleep(TimeSpan.FromSeconds(delaySecs));
        }

        static void Main(string[] args)
        {
            Console.Title = "The Secret Agent Test";
            Console.ForegroundColor = ConsoleColor.Green;
            WriteText("Welcome,agent. This is the test to see if\nyou are good enough to be a full member of the OT Secret Agency.","Do you want to continue? [Y/N]");
            var readk = Console.ReadKey();
            if (readk.Key == ConsoleKey.Y || readk.Key == ConsoleKey.N)
            {
                Console.Clear();
                WriteText("Let's continue the test.\n","Crack the password:\n","Username: IdioT_NOOB1337\nPROFILE: Likes memes such as doge.","Is an elitist (Over the things he likes)","Only uses the word idiot as an insult","Password:");
                string pass1 = Console.ReadLine();

                if (pass1 != "AnyoneWhodoesn'tLikeDogeIsAnIdiot" && pass1 != "anyonewhodoesn'tlikedogeisanidiot") return;

                WriteText(2,"Account accessed.","Stage 1 Complete.","Loading next level...");                
                WriteText(1,"Level 2 loaded.");                
                Console.Clear();
                WriteText(2.5,"Nice. You certainly have skill. But this test.... determines speed of mind.");                
                Console.Clear();
                Console.WriteLine("You only have two seconds to answer the next question. Press any key when ready.");
                Console.ReadKey();
                Console.Clear();
                Console.WriteLine("What is 12x12?!"); // QUESTION

                int allowedTime = 2 * 1000; // time allowed
                new Thread(() =>
                {
                    Stopwatch s = new Stopwatch();
                    s.Start();
                    while (s.ElapsedMilliseconds < allowedTime) { }
                    WriteText(2,"Sorry,you're too late. Restart the test again.");                    
                    Console.Clear();
                    Environment.Exit(-1);
                }).Start();

                string product = Console.ReadLine();
                if (product == "144") Console.WriteLine("Impressive. Your mind is fast,be prepared for the next test. Pressure.");

                WriteText(2,you are incorrect. Restart the test again.");                
                Console.Clear();                
            }
        }
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐