C
C#3y ago
Whiteboy

Thread.Sleep(int32) not working? Send Help [Answered]

private void BtnUpgrade_Click(object sender, EventArgs e)
{
if (IsEqScroll == false && IsEqAmulet == false)
{
var random = new Random();
var num = random.Next(1, 1000);
if(num < 1000/(1+ SharedClass.UpgradeLvlValue.UpgradeLevel*1.69))
{
lbUpgradeOutput.BackColor = Color.FromArgb(46, 51, 73);
Thread.Sleep(250);
SharedClass.UpgradeLvlValue.UpgradeLevel++;
lbUpgradeOutput.Text = "Upgrade succeed";
lbUpgradeOutput.BackColor = Color.Green;
UpgradeLvl.Text = "+ " + SharedClass.UpgradeLvlValue.UpgradeLevel.ToString();
}
else
{
lbUpgradeOutput.BackColor = Color.FromArgb(46, 51, 73);
Thread.Sleep(250);
lbUpgradeOutput.Text = "Upgrade failed";
lbUpgradeOutput.BackColor = Color.Red;
}
}
}
private void BtnUpgrade_Click(object sender, EventArgs e)
{
if (IsEqScroll == false && IsEqAmulet == false)
{
var random = new Random();
var num = random.Next(1, 1000);
if(num < 1000/(1+ SharedClass.UpgradeLvlValue.UpgradeLevel*1.69))
{
lbUpgradeOutput.BackColor = Color.FromArgb(46, 51, 73);
Thread.Sleep(250);
SharedClass.UpgradeLvlValue.UpgradeLevel++;
lbUpgradeOutput.Text = "Upgrade succeed";
lbUpgradeOutput.BackColor = Color.Green;
UpgradeLvl.Text = "+ " + SharedClass.UpgradeLvlValue.UpgradeLevel.ToString();
}
else
{
lbUpgradeOutput.BackColor = Color.FromArgb(46, 51, 73);
Thread.Sleep(250);
lbUpgradeOutput.Text = "Upgrade failed";
lbUpgradeOutput.BackColor = Color.Red;
}
}
}
So it goes like this - the basic color - 1st click on the button assigned - color goes green (succeeded) - 2nd click - color stays green (success again) When it should flash for 0.25 to the basic color then go green I want to make something aka flashy event for the label with text "upgrade succeeded/failed" so when you get 2x upgrade in a row you get that little visual information that it actually tried the upgrading instead of the color being green all time What am i missing?
10 Replies
Aeon Vex
Aeon Vex3y ago
I think the problem is you're sleeping on the UI thread, you should make the method async and then await Thread.Sleep(250);
Thinker
Thinker3y ago
You mean await Task.Delay(250);?
Whiteboy
WhiteboyOP3y ago
well i can't make async void i'll try it in a sec @thinker227 then i can't use it as an OnClick event for the button
Aeon Vex
Aeon Vex3y ago
yep sorry, brain scrambles
Whiteboy
WhiteboyOP3y ago
Warning CS4014 Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the 'await' operator to the result of the call.
Aeon Vex
Aeon Vex3y ago
why's that? admittedly been a while since I've used winforms (assuming that's what you're working in) but not finding anything that says callbacks can't be async
Whiteboy
WhiteboyOP3y ago
winforms .net framwork 4.7.2 w8 im dumb works thank you XD
Aeon Vex
Aeon Vex3y ago
👍
Whiteboy
WhiteboyOP3y ago
private async void BtnUpgrade_Click(object sender, EventArgs e)
{
if (IsEqScroll == false && IsEqAmulet == false)
{
var random = new Random();
var num = random.Next(1, 1000);
if(num < 1000/(1+ SharedClass.UpgradeLvlValue.UpgradeLevel*1.69))
{
lbUpgradeOutput.BackColor = Color.FromArgb(46, 51, 73);
await Task.Delay(25);
SharedClass.UpgradeLvlValue.UpgradeLevel++;
lbUpgradeOutput.Text = "Upgrade succeed";
lbUpgradeOutput.BackColor = Color.Green;
UpgradeLvl.Text = "+ " + SharedClass.UpgradeLvlValue.UpgradeLevel.ToString();
UpgradeMats();
}
else
{
lbUpgradeOutput.BackColor = Color.FromArgb(46, 51, 73);
await Task.Delay(25);
lbUpgradeOutput.Text = "Upgrade failed";
lbUpgradeOutput.BackColor = Color.Red;
}
}
}
private async void BtnUpgrade_Click(object sender, EventArgs e)
{
if (IsEqScroll == false && IsEqAmulet == false)
{
var random = new Random();
var num = random.Next(1, 1000);
if(num < 1000/(1+ SharedClass.UpgradeLvlValue.UpgradeLevel*1.69))
{
lbUpgradeOutput.BackColor = Color.FromArgb(46, 51, 73);
await Task.Delay(25);
SharedClass.UpgradeLvlValue.UpgradeLevel++;
lbUpgradeOutput.Text = "Upgrade succeed";
lbUpgradeOutput.BackColor = Color.Green;
UpgradeLvl.Text = "+ " + SharedClass.UpgradeLvlValue.UpgradeLevel.ToString();
UpgradeMats();
}
else
{
lbUpgradeOutput.BackColor = Color.FromArgb(46, 51, 73);
await Task.Delay(25);
lbUpgradeOutput.Text = "Upgrade failed";
lbUpgradeOutput.BackColor = Color.Red;
}
}
}
final version
Accord
Accord3y ago
✅ This post has been marked as answered!
Want results from more Discord servers?
Add your server