C# 将文本追加暂停一定的毫秒数

C# 将文本追加暂停一定的毫秒数,c#,multithreading,timer,C#,Multithreading,Timer,我目前正在制作一个基于文本的游戏,但我需要暂停一定数量的毫秒。我在找这样的东西: void InitProgram() { WriteToText("Welcome!"); CreatePause(3000); // Pause execution HERE for 3 seconds without locking UI WriteToText("How are you?"); // Continue StartTutorial(); } 因此,方法将被调用,执行等待

我目前正在制作一个基于文本的游戏,但我需要暂停一定数量的毫秒。我在找这样的东西:

void InitProgram()
{
   WriteToText("Welcome!");
   CreatePause(3000); // Pause execution HERE for 3 seconds without locking UI
   WriteToText("How are you?"); // Continue
   StartTutorial();
}
因此,方法将被调用,执行等待操作,然后返回。当它返回时,将继续正常执行。 对此效果我能做些什么?

您可以使用计时器:

readonly Timer _timer = new Timer();

void InitProgram()
{    
    WriteToText("Welcome!");

   _timer.Interval = 3000;
   _timer.Tick += timer_Tick;
   _timer.Start();
}


void timer_Tick(object sender, EventArgs e)
{
    WriteToText("How are you?"); // Continue
    StartTutorial();

    _timer.Stop();
}
如果您想多次调用此函数,只需将_timer.Start放入它自己的方法中,每次调用它时,3秒后会发生timer_Tick中的任何情况:

private void StartTimer()
{
    _timer.Start();
}

void timer_Tick(object sender, EventArgs e)
{
    WriteToText("How are you?"); // Continue
    StartTutorial();

    _timer.Stop();
}
您可以使用计时器:

readonly Timer _timer = new Timer();

void InitProgram()
{    
    WriteToText("Welcome!");

   _timer.Interval = 3000;
   _timer.Tick += timer_Tick;
   _timer.Start();
}


void timer_Tick(object sender, EventArgs e)
{
    WriteToText("How are you?"); // Continue
    StartTutorial();

    _timer.Stop();
}
如果您想多次调用此函数,只需将_timer.Start放入它自己的方法中,每次调用它时,3秒后会发生timer_Tick中的任何情况:

private void StartTimer()
{
    _timer.Start();
}

void timer_Tick(object sender, EventArgs e)
{
    WriteToText("How are you?"); // Continue
    StartTutorial();

    _timer.Stop();
}
您可以使用计时器:

readonly Timer _timer = new Timer();

void InitProgram()
{    
    WriteToText("Welcome!");

   _timer.Interval = 3000;
   _timer.Tick += timer_Tick;
   _timer.Start();
}


void timer_Tick(object sender, EventArgs e)
{
    WriteToText("How are you?"); // Continue
    StartTutorial();

    _timer.Stop();
}
如果您想多次调用此函数,只需将_timer.Start放入它自己的方法中,每次调用它时,3秒后会发生timer_Tick中的任何情况:

private void StartTimer()
{
    _timer.Start();
}

void timer_Tick(object sender, EventArgs e)
{
    WriteToText("How are you?"); // Continue
    StartTutorial();

    _timer.Stop();
}
您可以使用计时器:

readonly Timer _timer = new Timer();

void InitProgram()
{    
    WriteToText("Welcome!");

   _timer.Interval = 3000;
   _timer.Tick += timer_Tick;
   _timer.Start();
}


void timer_Tick(object sender, EventArgs e)
{
    WriteToText("How are you?"); // Continue
    StartTutorial();

    _timer.Stop();
}
如果您想多次调用此函数,只需将_timer.Start放入它自己的方法中,每次调用它时,3秒后会发生timer_Tick中的任何情况:

private void StartTimer()
{
    _timer.Start();
}

void timer_Tick(object sender, EventArgs e)
{
    WriteToText("How are you?"); // Continue
    StartTutorial();

    _timer.Stop();
}

像这样的怎么样

它的所有伪代码,我没有测试

Thread _thread;

void InitProgram()
{
   WriteToText("Welcome!");

   ThreadStart ts = new ThreadStart(StartThread);
   _thread = new Thread(ts);
   _thread.Start();
}

private void StartThread()
{
    Thread.CurrentThread.Sleep(3000);
    this.Invoke(delegate { this.StartTutorial(); });
}

private void StartTutorial()
{
   WriteToText("How are you?"); // Continue

   //Start tutorial
}

像这样的怎么样

它的所有伪代码,我没有测试

Thread _thread;

void InitProgram()
{
   WriteToText("Welcome!");

   ThreadStart ts = new ThreadStart(StartThread);
   _thread = new Thread(ts);
   _thread.Start();
}

private void StartThread()
{
    Thread.CurrentThread.Sleep(3000);
    this.Invoke(delegate { this.StartTutorial(); });
}

private void StartTutorial()
{
   WriteToText("How are you?"); // Continue

   //Start tutorial
}

像这样的怎么样

它的所有伪代码,我没有测试

Thread _thread;

void InitProgram()
{
   WriteToText("Welcome!");

   ThreadStart ts = new ThreadStart(StartThread);
   _thread = new Thread(ts);
   _thread.Start();
}

private void StartThread()
{
    Thread.CurrentThread.Sleep(3000);
    this.Invoke(delegate { this.StartTutorial(); });
}

private void StartTutorial()
{
   WriteToText("How are you?"); // Continue

   //Start tutorial
}

像这样的怎么样

它的所有伪代码,我没有测试

Thread _thread;

void InitProgram()
{
   WriteToText("Welcome!");

   ThreadStart ts = new ThreadStart(StartThread);
   _thread = new Thread(ts);
   _thread.Start();
}

private void StartThread()
{
    Thread.CurrentThread.Sleep(3000);
    this.Invoke(delegate { this.StartTutorial(); });
}

private void StartTutorial()
{
   WriteToText("How are you?"); // Continue

   //Start tutorial
}

如果目标框架为4.0或更高版本,IDE为VS2012或更高版本,则可以使用async/await

    private async void Foo()
    {
        Console.WriteLine("Going to Await");
        await Task.Delay(5000);
        Console.WriteLine("Done with awaiting");
    }

它非常简单和直接,最大的优点是保持“线性”流,因为必要的回调等由编译器自动处理。

如果目标框架为4.0或更高版本,IDE为VS2012或更高版本,则可以使用async/await

    private async void Foo()
    {
        Console.WriteLine("Going to Await");
        await Task.Delay(5000);
        Console.WriteLine("Done with awaiting");
    }

它非常简单和直接,最大的优点是保持“线性”流,因为必要的回调等由编译器自动处理。

如果目标框架为4.0或更高版本,IDE为VS2012或更高版本,则可以使用async/await

    private async void Foo()
    {
        Console.WriteLine("Going to Await");
        await Task.Delay(5000);
        Console.WriteLine("Done with awaiting");
    }

它非常简单和直接,最大的优点是保持“线性”流,因为必要的回调等由编译器自动处理。

如果目标框架为4.0或更高版本,IDE为VS2012或更高版本,则可以使用async/await

    private async void Foo()
    {
        Console.WriteLine("Going to Await");
        await Task.Delay(5000);
        Console.WriteLine("Done with awaiting");
    }

它非常简单明了,最大的优点是保持了“线性”流,因为必要的回调等都是由编译器自动处理的。

哈哈哈!我用可能是最疯狂的方法找到了答案!看看这个,伙计们

首先,声明全局列表:

private List<Action> actionList = new List<Action>();
private List actionList=new List();
现在,这是您在希望从中调用wait的方法中执行的操作:

WriteToLog("Hello!"); 
Action act = delegate() { WriteToLog("How are you?"); }; actionList.Add(act); // Create a new Action out of this method and add it to the action list!
CreatePause(3000); // Call the method with your specified time

void CreatePause(int millisecondsToPause)
    {
        Action w = delegate() { Thread.Sleep(millisecondsToPause); };
        for (int i = 0; i < actionList.Count; i++) // Iterate through each method in the action list that requires attention
        {
            Action a_Instance = (Action)actionList[i]; // Add a cast to each iteration
            AsyncCallback cb = delegate(IAsyncResult ar) { Invoke(a_Instance);  w.EndInvoke(ar); };   // Do each method!!!!
            w.BeginInvoke(cb, null);
        }
        actionList.Clear();  // Clear that list!
        return; // Return!
    }
WriteToLog(“你好!”);
Action act=delegate(){WriteToLog(“你好吗?”);};行动列表。添加(行动);//使用此方法创建一个新操作,并将其添加到操作列表中!
CreatePause(3000);//使用指定的时间调用该方法
void CreatePause(int毫秒停止暂停)
{
Action w=delegate(){Thread.Sleep(毫秒stopause);};
for(int i=0;i

老实说,这不应该奏效,但它确实奏效。

哈哈哈!我用可能是最疯狂的方法找到了答案!看看这个,伙计们

首先,声明全局列表:

private List<Action> actionList = new List<Action>();
private List actionList=new List();
现在,这是您在希望从中调用wait的方法中执行的操作:

WriteToLog("Hello!"); 
Action act = delegate() { WriteToLog("How are you?"); }; actionList.Add(act); // Create a new Action out of this method and add it to the action list!
CreatePause(3000); // Call the method with your specified time

void CreatePause(int millisecondsToPause)
    {
        Action w = delegate() { Thread.Sleep(millisecondsToPause); };
        for (int i = 0; i < actionList.Count; i++) // Iterate through each method in the action list that requires attention
        {
            Action a_Instance = (Action)actionList[i]; // Add a cast to each iteration
            AsyncCallback cb = delegate(IAsyncResult ar) { Invoke(a_Instance);  w.EndInvoke(ar); };   // Do each method!!!!
            w.BeginInvoke(cb, null);
        }
        actionList.Clear();  // Clear that list!
        return; // Return!
    }
WriteToLog(“你好!”);
Action act=delegate(){WriteToLog(“你好吗?”);};行动列表。添加(行动);//使用此方法创建一个新操作,并将其添加到操作列表中!
CreatePause(3000);//使用指定的时间调用该方法
void CreatePause(int毫秒停止暂停)
{
Action w=delegate(){Thread.Sleep(毫秒stopause);};
for(int i=0;i

老实说,这不应该奏效,但它确实奏效。

哈哈哈!我用可能是最疯狂的方法找到了答案!看看这个,伙计们

首先,声明全局列表:

private List<Action> actionList = new List<Action>();
private List actionList=new List();
现在,这是您在希望从中调用wait的方法中执行的操作:

WriteToLog("Hello!"); 
Action act = delegate() { WriteToLog("How are you?"); }; actionList.Add(act); // Create a new Action out of this method and add it to the action list!
CreatePause(3000); // Call the method with your specified time

void CreatePause(int millisecondsToPause)
    {
        Action w = delegate() { Thread.Sleep(millisecondsToPause); };
        for (int i = 0; i < actionList.Count; i++) // Iterate through each method in the action list that requires attention
        {
            Action a_Instance = (Action)actionList[i]; // Add a cast to each iteration
            AsyncCallback cb = delegate(IAsyncResult ar) { Invoke(a_Instance);  w.EndInvoke(ar); };   // Do each method!!!!
            w.BeginInvoke(cb, null);
        }
        actionList.Clear();  // Clear that list!
        return; // Return!
    }
WriteToLog(“你好!”);
Action act=delegate(){WriteToLog(“你好吗?”);};行动列表。添加(行动);//使用此方法创建一个新操作,并将其添加到操作列表中!
CreatePause(3000);//使用指定的时间调用该方法
void CreatePause(int毫秒停止暂停)
{
Action w=delegate(){Thread.Sleep(毫秒stopause);};
for(int i=0;i