线程无法按预期启动

php*_*exe 3 c# multithreading console-application

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

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.");
                    }
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

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

小智 6

您只创建了一个线程.你也应该开始吧.

System.Threading.Thread t = new System.Threading.Thread(timer);
t.Start();
Run Code Online (Sandbox Code Playgroud)


Jen*_*nsB 5

刚刚写下来作为一个例子,说明如何在不使用线程的情况下检查已经过了多长时间.

        bool isInTime = false;

        var start = DateTime.Now;
        Console.WriteLine("answer this in 5 seconds, what is 2x2");
        var answer = Console.ReadLine();

        if ((DateTime.Now - start).TotalSeconds <= 5)
            isInTime = true;

        if (isInTime && answer == "4")
            Console.WriteLine("Good job you are now an agent");
        else
            Console.WriteLine("To slow and too dumb");

        Console.ReadKey();
Run Code Online (Sandbox Code Playgroud)

秒表是另一种选择:http://www.dotnetperls.com/stopwatch

如果你真的想要线程(这个问题有点过分),这里有一些很好的例子:https://msdn.microsoft.com/en-us/library/ts553s52(v = vs1010).aspx