加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 00:25:34 所属栏目:百科 来源:网络整理
导读:我正在尝试进行测试,看看是否有某些技能. 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 Progra
我正在尝试进行测试,看看是否有某些技能.

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 ifnyou 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_NOOB1337nPROFILE: 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 ifnyou 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_NOOB1337nPROFILE: 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();                
            }
        }
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读