C#同时执行多个方法,方法并发

C#同时执行多个方法,方法并发,c#,concurrency,C#,Concurrency,这是我的第一个问题。 我正在为我的c#类构建一个简单的程序来模拟吃角子老虎机,我正在模拟列?用这段代码旋转效果 static void EfeitoJackpot() { string[] simbolos = new string[10] { "!", "#", "$", "%", "&", "=", "@", "~", "»", "«" }; Console.SetCursorPosition(25, 1);

这是我的第一个问题。 我正在为我的c#类构建一个简单的程序来模拟吃角子老虎机,我正在模拟列?用这段代码旋转效果

 static void EfeitoJackpot()
        {
            string[] simbolos = new string[10] { "!", "#", "$", "%", "&", "=", "@", "~", "»", "«" };
            Console.SetCursorPosition(25, 1);
            for (int i = 0; ; i++)
            {

                Console.Write(simbolos[i % 10] + "\b");


                System.Threading.Thread.Sleep(80); // velocidade

            }
        }
现在,我的问题是我想同时显示3次。我一直在读关于多线程和并行循环的书,但是经过很多努力,我还是被卡住了

总而言之,我希望这一切发生

              ** [ spinning ]    [ spinning ]    [ spinning ] **

这就是正在发生的事情

             ** [ spinning ] ..(method finishes executing)... [spinning] and so forth


既然你说你正在构建一个“简单的程序”,你就不需要多线程,因为它会使事情过于复杂。您可以只使用循环结构。大概是这样的:

const int MAX = 10;
string[] simbolos = new string[MAX] { "!", "#", "$", "%", "&", "=", "@", "~", "»", "«" };

// Start with some random positions for each column.
Random r = new Random();
int column1Index = r.Next(MAX);
int column2Index = r.Next(MAX);
int column3Index = r.Next(MAX);

// Track overall status, and status of each column.
bool keepSpinning = true;
bool spin1 = true, spin2 = true, spin3 = true;

while (keepSpinning)
{
    Console.WriteLine($"{simbolos[column1Index]} {simbolos[column2Index]} {simbolos[column3Index]}");

    if (spin1)
    {
        column1Index = column1Index < MAX - 1 ? column1Index + 1 : 0;
        spin1 = SomethingToDetermineIfColumnShouldKeepSpinning(1);
    }

    if (spin2)
    {
        column2Index = column2Index < MAX - 1 ? column2Index + 1 : 0;
        spin2 = SomethingToDetermineIfColumnShouldKeepSpinning(2);
    }

    if (spin3)
    {
        column3Index = column3Index < MAX - 1 ? column3Index + 1 : 0; 
        spin3 = SomethingToDetermineIfColumnShouldKeepSpinning(3);
    }
}
const int MAX=10;
string[]simbolos=新字符串[MAX]{“!”、“#”、“$”、“%”、“&”、“=”、“@”、“~”、“»”、“«”};
//从每个列的一些随机位置开始。
随机r=新随机();
int column1Index=r.Next(最大值);
int column2Index=r.Next(最大值);
int column3Index=r.Next(最大值);
//跟踪总体状态和每个列的状态。
bool keeppinning=true;
bool spin1=true,spin2=true,spin3=true;
同时(继续盘旋)
{
WriteLine($“{simbolos[column1Index]}{simbolos[column2Index]}{simbolos[column3Index]}”);
if(spin1)
{
column1Index=column1Index
别那么直截了当。仅仅因为有三个纺锤旋转并不意味着你需要三根线。如果您有CPU受限的问题,您只需要三个线程,而这不是——主要是等待,然后选择一个随机数。我建议你用一个线程来解决这个问题。