简体   繁体   中英

C# string Thread.Sleep()

Hi i'm currently using Thread.Sleep(200); in between text for it to look like old pokemon game with the text not appearing all at once but going a bit laggy word for word like this

Console.Write("help"); Thread.Sleep(200); Console.Write("me");

And i'm wondering if you can name/shorten the Thread.Sleep(200) to something like Ts200 . (I mean to text). So it's not much clutter. So it would be something like this with a string.

string Ts200
Ts200 = Thread.Sleep(200)
Console.Write("help"); Ts200; Console.Write("me")

But it's not working.

I get this error:

cannot implicitly convert type void to string

a simple way to do this would be to split the strings into arrays on spaces, then loop over them.

using System;
using System.Threading;

public static void slowType(string fullSentence, int millis_pause)
{
   string[] words = fullSentence.Split(' ');
   foreach(string s in words)
   {
       Console.Out.Write(s);
       Thread.Sleep(millis_pause);
       //we must reintroduce the space, since we were splitting on it.
       Console.Out.Write(' ');
   }
   //optionally ; start a new line at the end of the sentence
   Console.Out.Write("\r\n");
}

usage example :

public static void Main()
{
    string sentence = "i am a terribly slow typist";
    slowType(sentence, 200);
}

Try lambda if you don't mind () after Ts200 :

Action Ts200 = () => Thread.Sleep(200); 

...

Ts200();

However, even if it's possile Ts200() is far less readable than Thread.Sleep(200) , that's why don't do it.

You call the Thread.Sleep(200); on its own, it doesnt contain any value. So you cant return any value neither can you allocate it any value, except as the method parameter Thread.Sleep("Here belongs the method parameter, this method accepts an 'int' value") . The int method parameter sets for how many milli seconds you want to pause the mainthread.


Try something like this:

The main method is the entry point in your application once it gets executed. So when you start your application, you call the main method and execute it.

static void Main(string[] args)
{
    string sentence = "Here you enter your sentence...";
    string[] words = sentence.Split(' ');

    OutputDelayedText(words);
}

The string sentence is the string variable that contains the sentence you want to ouput on the console. So we split the sentence into single words with the method .Split(' ') . This splits the spring on each space ' ' and puts the single words into a string[] array. Note that you have to use in the .Split('') method these '' instead of what you are probably used to "" . Thats because the method accepts a char value and those '' indicate that you just put a single char in as the method parameter.

So in the end we call the OutputDelayedText() method with our word array as method parameter.

private static void OutputDelayedText(string[] myText)
{
    foreach (var word in myText)
    {
        Console.Write(word + " ");
        Thread.Sleep(200);
    }
}

Now we use a foreach loop to iterate over the array and write every single element of our array (one single word of our sentence) in one line on the console. If you are not familiar with foreach you could use either a for or while loop as well.

In generell i advice you to use Task.Delay() . Or in WinForms the Backgroundworker , those will allow you to work on different threads and if you pause those threads you dont pause your main thread. You will come across this issue later, this is used to pause any process or to execute a long task in the background to not freeze your GUI when the task is still running.

However just start with Thread.Sleep() , but keep in mind: In the art of programming are so many ways how to achieve your goal and you might stumble quite often over a better solution.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM