Reputation: 258
I'm new on this of C#, I don't know so much of it.
I'm trying to make an app that changes a TextBlock every second. The app is UWP and I'm using C#.
This is my code:
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
}
public bool stop = false;
private void Button_Click(object sender, RoutedEventArgs e)
{
string input = TextInput.Text + " ";
TextOutput.Text = input;
string output = TextOutput.Text;
for (int i = 0; i <50; i++)
{
output = output.Substring(1, output.Length - 1) + output[0];
TextOutput.Text = output;
Task.Delay(150);
}
}
}
For now I've tryed Task.Delay()
, Thread.Sleep()
and others, but this doesn't stop the for-loop.
In fact, I've made the same app but for console, and the Thread.Sleep() works perfectly.
Can you help me, please?
Upvotes: 1
Views: 755
Reputation: 67362
You were pretty close, it's just that Task.Delay
is a task, perhaps obviously. And you have to await (or continue) tasks:
private async void Button_Click(object sender, RoutedEventArgs e)
{
string input = TextInput.Text + " ";
TextOutput.Text = input;
string output = TextOutput.Text;
for (int i = 0; i <50; i++)
{
output = output.Substring(1, output.Length - 1) + output[0];
TextOutput.Text = output;
await Task.Delay(150);
}
}
Upvotes: 2
Reputation: 637
the value you are passing into Task.Delay / Thread.Sleep is going be milliseconds not seconds.
I would try Thread.sleep(1000) // this will you a delay of 1 second.
Upvotes: 0