How to delay for loop cycles? - c#

I have a for loop where I calculate the value of something and display it, for example
void Button_Click(object sender, RoutedEventArgs e)
{
InitializeComponent();
Random rand = new Random();
for (i = 0; i <= 30; i++)
{
rnd = rand.Next(1,10);
value += i + rnd;
display.Content = value;
}
}
The problem is, I want to see the value overwritten after each cycle, delayed by X seconds, how could I achieve that in WPF?

Use Task.Delay to execute some code after a period of time.
private async void Button_Click(object sender, RoutedEventArgs e)
{
InitializeComponent();
Random rand = new Random();
for (i = 0; i <= 30; i++)
{
rnd = rand.Next(1,10);
value += i + rnd;
display.Content = value;
await Task.Delay(Timespan.FromSeconds(1));
}
}

Related

Generate random numbers till Button click (UWP)

I have a textblock and a button.
I want the textblock to be filled with a random number when I click the button and it should change the random number every 5 seconds. When I click the button a second time it should stop at the last random number.
How do I make this? This is what I tried:
bool thisStatus = false;
private void btn_click(object sender, RoutedEventArgs e)
{
if (thisStatus == false)
{
thisStatus = true;
}
else thisStatus = false;
random();
}
private void random()
{
while (thisStatus)
{
Random random = new Random();
int RandomNumber = random.Next(0, 100);
txtBlck.Text = RandomNumber.ToString();
Task.Delay(5000);
}
}
bool thisStatus = false;
private async void btn_click(object sender, RoutedEventArgs e)
{
thisStatus = !thisStatus // mke the toggling simple.
await random();
}
private async Task random()
{
while (thisStatus)
{
Random random = new Random();
int RandomNumber = random.Next(0, 100);
txtBlck.Text = RandomNumber.ToString();
await Task.Delay(5000);
}
}
you need to await the task delay method because awaiting makes sure it completes before moving on.
You can use the dispatcher Timer
dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
dispatcherTimer.Interval = new TimeSpan(0,0,5);
int RandomNumber;
Random random = new Random();
private void btn_click(object sender, RoutedEventArgs e)
{
if (thisStatus == false)
{
thisStatus = true;
dispatcherTimer.Start();
}
else {
thisStatus = false;
dispatcherTimer.Stop();
}
}
private void dispatcherTimer_Tick(object sender, EventArgs e)
{
RandomNumber = random.Next(0, 100);
}

Incrementing Text in Circular Progress Bar

private void _btnOK_Click(object sender, EventArgs e)
{
_label1.Hide();
_label2.Hide();
_label3.Hide();
for(int i = 1; i <= 100; i++)
{
Thread.Sleep(5);
_circularprogressbar.Value = i;
_circularprogressbar.Update();
}
}
private void LoadingScreen_Load(object sender, EventArgs e)
{
_circularprogressbar.Value = 0;
_circularprogressbar.Minimum = 0;
_circularprogressbar.Maximum = 100;
}
}
}
This is my code. What i want to do is, i want to have a text inside the progress bar that shows the percentage of the progress from 1 to 100 percent.
what can i add to my code?
thank you
Here is what i would do:
private void _btnOK_Click(object sender, EventArgs e)
{
_label1.Hide();
_label2.Hide();
_label3.Hide();
for(int i = 1; i <= 100; i++)
{
_circularprogressbar.Value = i;
_percent_lable_name.Text = string.Format("{0}%", _circularprogressbar.Value);
_circularprogressbar.Update();
}
}
private void LoadingScreen_Load(object sender, EventArgs e)
{
_circularprogressbar.Value = 0;
_circularprogressbar.Minimum = 0;
_circularprogressbar.Maximum = 100;
}
}
See if that helps you!
Thanks
Techcraft7 :)
That Thread.Sleep(5) is blocking your entire UI thread. If you want to have your UI responsive, while the progress takes place, you need to make a separate thread for it. Something like this:
private void _btnOK_Click(object sender, EventArgs e)
{
_label1.Hide();
_label2.Hide();
_label3.Hide();
Task.Factory.StartNew(() =>
{
for (int i = 1; i <= 100; i++)
{
Thread.Sleep(5);
Invoke((Action)(() =>
{
_circularprogressbar.Value = i;
_circularprogressbar.Update();
}));
}
});
}
Note that you will need t use Invoke to BeginInvoke to access UI components from inside that thread.

visual studio timer ticks displayed?

This is my code for my drag race but I am wondering, I have my timer but how do I place it on a label?
private void tmrRaceTimerNamo_Tick(object sender, EventArgs e)
{
//car speed
pcbCar1Namo.Left = pcbCar1Namo.Left + 10;
pcbCar2Namo.Left = pcbCar2Namo.Left + 4;
pcbCar3Namo.Left = pcbCar3Namo.Left + 5;
pcbCar4Namo.Left = pcbCar4Namo.Left + 7;
//car stops at finish
AllCarsOnFinishNamo();
}
private void AllCarsOnFinishNamo()
{
if (pcbCar1Namo.Left > pcbFininshNamo.Right)
{
pcbCar1Namo.Left = pcbFininshNamo.Right;
}
if (pcbCar2Namo.Left > pcbFininshNamo.Right)
{
pcbCar2Namo.Left = pcbFininshNamo.Right;
tmrRaceTimerNamo.Enabled = false;
}
if (pcbCar3Namo.Left > pcbFininshNamo.Right)
{
pcbCar3Namo.Left = pcbFininshNamo.Right;
}
if (pcbCar4Namo.Left > pcbFininshNamo.Right)
{
pcbCar4Namo.Left = pcbFininshNamo.Right;
}
}
private void btnGoNamo_Click(object sender, EventArgs e)
{
//start of timer
tmrRaceTimerNamo.Enabled = true;
}
You can simply set the label to the value on each tick or update. For example in your tmrRaceTimerNamo_Tick(object sender, EventArgs e) method, you can set the labels text property to the time.
I dont exactly know what you want to show but i think you want to see the fps you are getting while playing your drag race game so here is a function for afps counter
private static int lastTick;
private static int lastFrameRate;
private static int frameRate;
public static int CalculateFrameRate()
{
label.Text = CalculateFrameRate().ToString();
if (System.Environment.TickCount - lastTick >= 1000)
{
lastFrameRate = frameRate;
frameRate = 0;
lastTick = System.Environment.TickCount;
}
frameRate++;
return lastFrameRate;
}

I need my random values to look like they are rolling C#

I have five dice labels and I have a method Roll() that sets their
text value. At the moment when the roll method happens it just makes the numbers appear, but I would like them to look like they are rolling.
Here is what I have
//Roll() simulates the rolling of this die.
public void Roll()
{
if (Active) {
faceValue = random.Next(1, 7);
label.Text = faceValue.ToString();
}
}
Roll() is called from another class like this:
for (int i = 0; i < dice.Length; i++){
dice[i].Roll();
}
My Question is:
How can I let it looks like they are rolling through a set of numbers then stop on a number ?
Try this
public async void Roll()
{
if (Active)
{
for (int i=0;i<20;i++) //Number of rolls before showing final
{
await Task.Delay(100);
label.Text = random.Next(1, 7).ToString();
}
}
}
I tested a Little and this Looks good done in WPF with a DispatcherTimer:
DispatcherTimer timer = new DispatcherTimer();
private void Button_Click(object sender, RoutedEventArgs e)
{
timer.Tick += Roll;
timer.Interval = new TimeSpan(1);
Random r = new Random();
timer.Start();
}
Random r = new Random();
public void Roll(object sender, EventArgs e)
{
increment++;
if (increment >= 250)
{
timer.Stop();
increment = 0;
}
DiceLabel.Content = r.Next(1, 7).ToString();
timer.Interval = new TimeSpan(increment*3000);
}
If you want it faster or longer you can play with the value from:
timer.Interval = new TimeSpan(increment*3000);
here to set the time between the new number is shown.
And for the time to Dice is rolling, you can play with:
if (increment >= 250)
Hope that helps, have fun.

increment how many times an image is displayed in a picturebox

I'm trying to display an image using a button click and increment a variable when a certain image is shown, but with the code below the variable num is always 0.
my code
int num = 0;
int i = 0;
int x = 0;
PictureBox[] pictureBoxs = new PictureBox[4];
Random rnd = new Random();
public UserControl1()
{
InitializeComponent();
pictureBoxs[0] = pbimg1;
pictureBoxs[1] = pbimg2;
pictureBoxs[2] = pbimg3;
pictureBoxs[3] = pbimg4;
x = rnd.Next(2);
}
public void displaypics()
{
pictureBoxs[i].Image = imageList1.Images[x];
}
private void btn2_Click(object sender, EventArgs e)
{
i=1;
displaypics();
if (pictureBoxs[i].Image == imageList1.Images[1])
{
num++;
}
if (num == 2)
{
tb1.Visible = true;
tb1.Text = "GAME OVER!" + num;
}
}
The reason is most likely that num is being instantiated to zero everytime the class is being instantiated
What happens when you set breakpoints and step through the code? Is the int set as 0, or does it contain the updated value?
I'm not sure what the context is in which that piece of code is used. So I guess what should solve this would be adding x = rnd.Next(2) to the btn2_Click method. Making it look like this:
private void btn2_Click(object sender, EventArgs e)
{
x = rnd.Next(2);
displaypics();
if (pictureBoxs[i].Image == imageList1.Images[1])
{
num++;
}
if (num == 2)
{
tb1.Visible = true;
tb1.Text = "GAME OVER!" + num;
}
i++;
}
Maybe you could give some more details on what that control should do/how it's used.

Categories