Chào mừng đến với Diễn đàn lập trình - Cộng đồng lập trình.
Kết quả 1 đến 7 của 7
  1. #1
    Ngày tham gia
    Sep 2015
    Bài viết
    0

    làm sao để thời gian chạy song song với 1 hàm khác

    e có 1 hàm đếm thừi gian như sau
    Mã:
    {
            int t = 90;
    
            Thread.Sleep(1000);
            while (t >= 1)
            {
                t--;
                Thread.Sleep(1000);
                Console.SetCursorPosition(55,1);
                Console.Write(t);
            }
    và 1 hàm level như sau
    Mã:
     public void Level1()
        {
            int j = 0;
            int l = 0;
    	
            
    
            
    
    
    
    
            Console.Clear();
            do
            {
                Console.SetCursorPosition(45, 1);
                Console.WriteLine("Marks: ");
                Console.SetCursorPosition(53, 1);
                Console.WriteLine(RecordPoints);
                Console.Clear();
                int Chances=9;
                string Name = "countryside";
                Console.SetCursorPosition(0, 1);
                Console.Write("Chances: ");
                Console.SetCursorPosition(10, 1);
                Console.WriteLine(Chances);
                Console.SetCursorPosition(45, 1);
                Console.WriteLine("Marks: ");
                Console.SetCursorPosition(0, 15);
    
    
    
                Console.WriteLine("Total Characters: {0}", Name.Length);
                string[] array = new string[Name.Length];
                int so = 0;
                for (int i = 0; i < Name.Length; i++)
                {
                    string len = Name.Substring(i, 1);
                    array[so] = len;
                    so++;
                }
                Random auto = new Random();
                int value = auto.Next(so);
                //----------------------------------------/
                int point = (50 - (Name.Length));
                int diem = 0;
                for (int i = 0; i < Name.Length; i++)
                {
                    if (array[i] == array[value])
                    {
                        Console.SetCursorPosition(point, 5);
                        Console.WriteLine(array[value]);
                        diem++;
                    }
                    else
                    {
                        Console.SetCursorPosition(point, 5);
                        Console.WriteLine("_");
                    }
                    point += 2;
                }
                //----------------------/
                j = (Name.Length) - diem;
                int Loop = 0;
                Console.SetCursorPosition(2, 10);
                Console.Write("Guess: ");
                while (Loop < Chances)
                {
                    Console.SetCursorPosition(9, 10);
                    string Guess = Console.ReadLine();
                    int point2 = (50 - (Name.Length));
                    for (int i = 0; i < Name.Length; i++)
                    {
                        if (Guess == array[value])
                        {
                            break;
                        }
                        if (Guess == array[i])
                        {
                            for (int y = i; y < Name.Length; y++)
                            {
                                if (Guess == array[y])
                                {
                                    Console.SetCursorPosition(point2, 5);
                                    Console.WriteLine(Guess);
                                    l++;
                                    RecordPoints++;
                                    Console.SetCursorPosition(53, 1);
                                    Console.WriteLine(RecordPoints);
                                }
                                point2 += 2;
                            }
                            break;
                        }
    
                        if (i == (Name.Length - 1))
                        {
                            Loop++;
                            int s = (Chances - Loop);
                            Console.SetCursorPosition(10, 1);
                            Console.WriteLine(s);
                            if (Loop == Chances)
                            {
                                Console.Clear();
                                Console.SetCursorPosition(30, 3);
                                Console.WriteLine("GAME OVER!");
                            }
                            break;
                        }
                        point2 += 2;
                    }
                    if (j == l)
                    {
                        break;
                    }
                }
            } while (j == l);
        }
    vậy làm sao để 2 hàm trên cùng chạy 1 lúc ạ
    em làm nó cứ 1 cái chạy trước 1 cái chạy sau

  2. #2
    Dùng multi-thread đi bạn [IMG]images/smilies/biggrin.png[/IMG]

  3. #3
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    dùng như thế nào hả bạn
    mình mới học nên chưa biết nhiều lắm

  4. #4
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    Mã:
    System.Threading.Thread thread = new System.Threading.Thread(<hàm cần chạy>);thread.Start();

  5. #5
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    mọi người chỉ cụ thể hơn được ko ạ
    em viết hàm main như sau

    Mã:
    class Program
    {
       
        static void Main(string[] args)
        {
            GameVH obj = new GameVH();
            obj.Guide();
            obj.Name();
          
            
    	System.Threading.Thread thread = new System.Threading.Thread(Level1);
    	thread.Start();
    	int t = 90;
            Thread.Sleep(1000);
            while (t >= 1)
            {
                t--;
                Thread.Sleep(1000);
                Console.SetCursorPosition(55, 1);
                Console.Write(t);
            } 
    
    
        }
    }
    nhưng chạy bị lỗi

  6. #6
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    Cái phương thức Level1() bạn đặt ở trong class Program luôn hả, có phải static ko. Còn phần này

    Mã:
    	int t = 90;
            Thread.Sleep(1000);
            while (t >= 1)
            {
                t--;
                Thread.Sleep(1000);
                Console.SetCursorPosition(55, 1);
                Console.Write(t);
            }
    nên cho nó vào một phương thức riêng để xài thread

  7. #7
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    Làm thế này nè bạn
    Mã nguồn PHP:
    using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading;namespace ConsoleApplication2{ class Program { public static void Main(string[] args) { GameVH obj = new GameVH(); obj.Guide(); obj.Name(); Thread thread1 = new Thread(new ThreadStart(Level1)); thread1.Start(); int t = 90; Thread.Sleep(1000); while (t >= 1) { t--; Thread.Sleep(1000); Console.SetCursorPosition(55, 1); Console.Write(t); } } public static void Level1() { int j = 0; int l = 0; Console.Clear(); do { Console.SetCursorPosition(45, 1); Console.WriteLine("Marks: "); Console.SetCursorPosition(53, 1); Console.WriteLine(RecordPoints); Console.Clear(); int Chances = 9; string Name = "countryside"; Console.SetCursorPosition(0, 1); Console.Write("Chances: "); Console.SetCursorPosition(10, 1); Console.WriteLine(Chances); Console.SetCursorPosition(45, 1); Console.WriteLine("Marks: "); Console.SetCursorPosition(0, 15); Console.WriteLine("Total Characters: {0}", Name.Length); string[] array = new string[Name.Length]; int so = 0; for (int i = 0; i < Name.Length; i++) { string len = Name.Substring(i, 1); array[so] = len; so++; } Random auto = new Random(); int value = auto.Next(so); //----------------------------------------/ int point = (50 - (Name.Length)); int diem = 0; for (int i = 0; i < Name.Length; i++) { if (array[i] == array[value]) { Console.SetCursorPosition(point, 5); Console.WriteLine(array[value]); diem++; } else { Console.SetCursorPosition(point, 5); Console.WriteLine("_"); } point += 2; } //----------------------/ j = (Name.Length) - diem; int Loop = 0; Console.SetCursorPosition(2, 10); Console.Write("Guess: "); while (Loop < Chances) { Console.SetCursorPosition(9, 10); string Guess = Console.ReadLine(); int point2 = (50 - (Name.Length)); for (int i = 0; i < Name.Length; i++) { if (Guess == array[value]) { break; } if (Guess == array[i]) { for (int y = i; y < Name.Length; y++) { if (Guess == array[y]) { Console.SetCursorPosition(point2, 5); Console.WriteLine(Guess); l++; RecordPoints++; Console.SetCursorPosition(53, 1); Console.WriteLine(RecordPoints); } point2 += 2; } break; } if (i == (Name.Length - 1)) { Loop++; int s = (Chances - Loop); Console.SetCursorPosition(10, 1); Console.WriteLine(s); if (Loop == Chances) { Console.Clear(); Console.SetCursorPosition(30, 3); Console.WriteLine("GAME OVER!"); } break; } point2 += 2; } if (j == l) { break; } } } while (j == l); } }}  

 

 

Quyền viết bài

  • Bạn Không thể gửi Chủ đề mới
  • Bạn Không thể Gửi trả lời
  • Bạn Không thể Gửi file đính kèm
  • Bạn Không thể Sửa bài viết của mình
  •