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

c# – 线程无法按预期启动,第1张

概述我正在尝试进行测试,看看是否有某些技能. using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace ConsoleApplication2{ class timerup { 我正在尝试进行测试,看看是否有某些技能.

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();                            }        }    }}
总结

以上是内存溢出为你收集整理的c# – 线程无法按预期启动全部内容,希望文章能够帮你解决c# – 线程无法按预期启动所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1223965.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-05
下一篇 2022-06-05

发表评论

登录后才能评论

评论列表(0条)

保存