C
C#2y ago
valera456

❔ What did I do wrong with this task formation?

I want to make an infinite method that generates a random number for a "slot machine". I also have to be able to stop it upon click of a button to finalize a number. The vital code is this:
public async Task Numbers(TextBox textbox, CancellationToken token)
{
Random rnd = new Random();
while (token.IsCancellationRequested == false)
{
textbox.Text = rnd.Next(1, 10).ToString();
await Task.Delay(100);
}
}

public async void Check(TextBox textbox, Button button)
{
CancellationTokenSource cts = new CancellationTokenSource();
if (button.Text == "Launch")
{
Task.Run(() => Numbers(textbox, cts.Token), cts.Token);
button.Text = "Stop";
}
else if (button.Text == "Stop")
{
cts.Cancel();
button.Text = "Launch";
}
}
public async Task Numbers(TextBox textbox, CancellationToken token)
{
Random rnd = new Random();
while (token.IsCancellationRequested == false)
{
textbox.Text = rnd.Next(1, 10).ToString();
await Task.Delay(100);
}
}

public async void Check(TextBox textbox, Button button)
{
CancellationTokenSource cts = new CancellationTokenSource();
if (button.Text == "Launch")
{
Task.Run(() => Numbers(textbox, cts.Token), cts.Token);
button.Text = "Stop";
}
else if (button.Text == "Stop")
{
cts.Cancel();
button.Text = "Launch";
}
}
4 Replies
valera456
valera456OP2y ago
the function Numbers() doesnt work when a part of task. if i put it as is it works. what am i supposed to do then i repurposed it to
CancellationTokenSource cts1 = new CancellationTokenSource();
CancellationTokenSource cts2 = new CancellationTokenSource();
CancellationTokenSource cts3 = new CancellationTokenSource();
public async Task Numbers(TextBox textbox, CancellationToken token)
{
Random rnd = new Random();
while (token.IsCancellationRequested==false)
{
textbox.Text = rnd.Next(1, 10).ToString();
await Task.Delay(100);
}
}
public async void Check(TextBox textbox, Button button, CancellationTokenSource cts)
{
if (button.Text=="Пуск")
{
var task = Task.Run(()=>Numbers(textbox, cts.Token),cts.Token);
button.Text = "Стоп";
}
else if (button.Text=="Стоп")
{
cts.Cancel();
button.Text = "Пуск";
}
if (button1.Text=="Пуск" && button2.Text == "Пуск" && button3.Text == "Пуск")
{
textBox1.Text = Determine(textBox2, textBox3, textBox4);
}
}
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
Check(textBox2, button1, cts1);
}

private void button2_Click(object sender, EventArgs e)
{
Check(textBox3, button2, cts2);
}

private void button3_Click(object sender, EventArgs e)
{
Check(textBox4, button3, cts3);
}
CancellationTokenSource cts1 = new CancellationTokenSource();
CancellationTokenSource cts2 = new CancellationTokenSource();
CancellationTokenSource cts3 = new CancellationTokenSource();
public async Task Numbers(TextBox textbox, CancellationToken token)
{
Random rnd = new Random();
while (token.IsCancellationRequested==false)
{
textbox.Text = rnd.Next(1, 10).ToString();
await Task.Delay(100);
}
}
public async void Check(TextBox textbox, Button button, CancellationTokenSource cts)
{
if (button.Text=="Пуск")
{
var task = Task.Run(()=>Numbers(textbox, cts.Token),cts.Token);
button.Text = "Стоп";
}
else if (button.Text=="Стоп")
{
cts.Cancel();
button.Text = "Пуск";
}
if (button1.Text=="Пуск" && button2.Text == "Пуск" && button3.Text == "Пуск")
{
textBox1.Text = Determine(textBox2, textBox3, textBox4);
}
}
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
Check(textBox2, button1, cts1);
}

private void button2_Click(object sender, EventArgs e)
{
Check(textBox3, button2, cts2);
}

private void button3_Click(object sender, EventArgs e)
{
Check(textBox4, button3, cts3);
}
It still does not display any numbers on initial press. i dont know? lemme see i did
if (task.Status==TaskStatus.Running)
{
MessageBox.Show("Yes");
}
if (task.Status==TaskStatus.Running)
{
MessageBox.Show("Yes");
}
its not showing anything so its not running but when i try to do task.Start() it says that its running on promise-style so when does it run? what do i check for exactly? in the locals or? yes it does stop at the breakpoint. and it does get deeper into the actual number set of a textbox. the application does not reflect that for some reason
valera456
valera456OP2y ago
by that i mean that no numbers are showing (they are showing if i call numbers() without the task)
valera456
valera456OP2y ago
so i have to out an int for that? its now working correctly! thanks
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server