多线程问题 [英] Issue with Multi-Threading

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

问题描述

我有这两种方法

    public void L1Timer()
    {
        Console.Clear();
        int score = tot;
        Console.Write("Chances : " + ch);
        Console.CursorLeft = 40;
        Console.Write("Marks : " + score);
        for (int time = 0; time <= 100000; time++)
        {
            Console.SetCursorPosition(65, 0);
            Console.Write("Time Elapsed : " + time + " Secs");
            Console.CursorLeft = 40;
            stime = time;
            Thread.Sleep(1000);
            Console.Beep();
            //Level1();
        }
    }

    public void Level1()
    {
        Console.WriteLine("\n\n");
        Console.CursorLeft = 40;
        Console.WriteLine("C _ _ E _ _ _ T _ _ N");
        Console.WriteLine("\n\n");
        tot = 0;
        while ((tot <= 70) && (ch > 0))
        {
            Console.Write("Guess : ");
            string gues = Console.ReadLine();
            switch (gues)
            {
                case "E": tot += 10; ch--; L1Timer(); Level1(); break;
                case "L": tot += 10; ch--; break;
                case "B": tot += 10; ch--; break;
                case "R": tot += 10; ch--; break;
                case "A": tot += 10; ch--; break;
                case "I": tot += 10; ch--; break;
                case "O": tot += 10; ch--; break;
                default: tot += 0; ch--; break;
            }
            Console.WriteLine();
        }
    }

我想先执行第一个方法,然后在第一个方法计时器仍在计数而又不延迟第二个线程的情况下开始执行第二个方法...

I want to Execute the first method first and then Start Executing the second with the first method timer still counting without delaying the Second Thread... I have tried this

            Thread T1 = new Thread(new ThreadStart(L1Timer));

            Thread T2 = new Thread(new ThreadStart(Level1));

            T1.Start();

            T2.Start();

但是它并没有按照我想要的方式工作..它摘录了第一和第二个参数,但是将光标移回了第一个方法,这不允许用户键入第二个方法并回答问题...请帮我

But it didn't work the way i want it..It excuted the first and second but took back the cursor to the first method which will not allow the user to type in the second method and answer the question...Pls help me out

推荐答案

以下是基本概念:

运行它,然后在右上方观看经过的时间.打一些字母,检查分数/机会.按下Escape键,然后按"q"或"r"键,看看会发生什么情况.

Run it and watch the time elapsed in the upper right. Hit some letters and check the score/chances. Hit Escape then 'q' or 'r' and see what happens.

这只是一个简单的示例,用于演示高级"控制台界面的流程. 对于您尚未在原始问题描述中以某种方式发布的游戏",我几乎没有提出任何真正的逻辑.因此,我不认为这是为您做功课".这只是游戏中的绒毛".一个真正的任务可能会让您将游戏逻辑放入 Class 中.如果像我对"Score"和"Chances"变量所做的那样,将该类实例声明为静态实例,则除Main()之外的其他例程都可以访问它,并使用类游戏状态"中的值更新屏幕.开发代表董事会和当前游戏状态的班级通常是任务的核心,也是您真正应该专注的地方.

This is just a simple example to demonstrate the flow for a "fancy" console interface. I've put almost no real logic behind this "game" that you didn't already post in some fashion in your original question description; thus I don't consider this as "doing your homework for you." This is just "fluff" on top of a game. A real assignment will probably have you put the game logic into a Class. If you declare that class instance as static like I've done with the "Score" and "Chances" variables then the different routines besides Main() can access it and update the screen using values from your classes "game state". Developing the Class to represent the Board and the current Game State is usually the core of the assignment and where your focus should really lie.

希望有帮助...

class Program
{

    static int Score = 0;
    static int Chances = 10;
    static int promptX, promptY;
    static string board = "{No Board Loaded}";
    static System.Diagnostics.Stopwatch SW = new Stopwatch();

    static void Main(string[] args)
    {
        ConsoleKeyInfo cki;
        Console.Title = "Some Word Game";

        bool quit = false;
        bool gameWon = false;
        bool gameOver = false;

        while (!quit)
        {
            Reset();
            ShowBoard("C _ _ E _ _ _ T _ _ N");

            gameWon = false;
            gameOver = false;
            while (!gameOver)
            {
                UpdateStats();

                // make it appear as though the cursor is waiting after the prompt:
                Console.SetCursorPosition(promptX, promptY);
                if (Console.KeyAvailable)
                {
                    cki = Console.ReadKey(true); // don't display key
                    if (cki.Key == ConsoleKey.Escape)
                    {
                        gameOver = true;
                    }
                    else
                    {
                        // if it's A thru Z...
                        if (char.IsLetter(cki.KeyChar)) 
                        {
                            string key = cki.KeyChar.ToString().ToUpper();
                            Console.Write(key);
                            switch (key)
                            {
                                case "E":
                                    Score += 10;
                                    Chances--;
                                    break;

                                case "L":
                                    Score += 10;
                                    Chances--;
                                    break;

                                case "B":
                                    Score += 10;
                                    Chances--;
                                    break;

                                case "R":
                                    Score += 10;
                                    Chances--;
                                    break;

                                case "A":
                                    Score += 10;
                                    Chances--;
                                    break;

                                case "I":
                                    Score += 10;
                                    Chances--;
                                    break;

                                case "O":
                                    Score += 10;
                                    Chances--;
                                    break;

                                default:
                                    Score += 0;
                                    Chances--;
                                    break;
                            }

                            // ... possibly update the board somehow in here? ... 
                            // ... some board logic ...
                            // ShowBoard("updated board in here");

                            // set gameOver to drop out of loop
                            // also set gameWon if the user beat the board

                        }
                        else
                        {
                            Console.Write(" ");
                        }

                    }
                }

                System.Threading.Thread.Sleep(200);
            }

            if (gameWon)
            {
                // ... do something ...
            }
            else
            {
                // ... do something else ...
            }

            quit = QuitProgram();
        }
    }

    static void Reset()
    {
        // reset game variables and clock:
        Score = 0;
        Chances = 10;
        SW.Restart();

        Console.Clear();
        CenterPrompt("Guess: ");
        promptX = Console.CursorLeft;
        promptY = Console.CursorTop;
    }

    static void ShowBoard(string NewBoard)
    {
        board = NewBoard;
        Console.SetCursorPosition(Console.WindowWidth / 2 - board.Length / 2, promptY - 2);
        Console.Write(board);
    }

    static void UpdateStats()
    {
        // hide cursor while we update the stats:
        Console.CursorVisible = false;

        Console.SetCursorPosition(0, 0);
        Console.Write("Score: " + Score.ToString("000"));

        Console.SetCursorPosition(35, 0);
        Console.Write("Chances: " + Chances.ToString("00"));

        TimeSpan ts = SW.Elapsed;
        string totalTime = String.Format("Time Elapsed: {0}:{1}:{2}", ts.Hours, ts.Minutes.ToString("00"), ts.Seconds.ToString("00"));
        Console.SetCursorPosition(Console.WindowWidth - totalTime.Length, 0);
        Console.Write(totalTime);

        // ... add other output statistics in here? ...

        // turn cursor back on for the prompt:
        Console.CursorVisible = true;
    }

    static void CenterPrompt(string message)
    {
        Console.SetCursorPosition(Console.WindowWidth / 2 - message.Length / 2, Console.WindowHeight / 2);
        Console.Write(message);
    }

    static bool QuitProgram()
    {
        Console.Clear();
        CenterPrompt("Thanks for playing! Press 'q' to Quit, or 'r' to Retry.");
        while (true)
        {
            if (Console.KeyAvailable)
            {
                ConsoleKeyInfo cki = Console.ReadKey(true);
                switch (cki.KeyChar.ToString().ToUpper())
                {
                    case "Q":
                        return true;

                    case "R":
                        return false;

                }
            }
        }
    }

}

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

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