This question already has answers here:
How to read/write on console at the same time using threads
(2 answers)
How do I "Pause" a console application when the user presses escape?
(2 answers)
Closed 1 year ago.
I am currently working on a school project that requires data to be generated, processed and stored every 2 seconds. For that, I am using while(true) loop. The problem I have is allowing user to break that loop and request generated data. Generating/storing has to be a continuous process that happens in the background, so I can't ask user for input at the beginning of each iteration. I hope someone could help me out. This would be a simple example:
int i = 0;
List<int> list = new List<int>();
//Console.WriteLine("Generating will continue until any key is pressed... ");
while (true)
{
i++;
li.Add(i);
Thread.Sleep(2000);
}
foreach (int i in list)
{
Console.Writeline(i);
}
while(!Console.KeyAvailable)
{
//do work
}
Related
This question already has answers here:
Captured variable in a loop in C#
(10 answers)
Thread parameters being changed
(2 answers)
Closed 2 years ago.
I would like to add onClick listeners to my buttons in Unity, C#.
I have an array of these buttons. I am trying to loop through that array, and for each button, I am adding an onClick listener which calls a function that takes an index parameter.
In that function, I print out the paramater that the function has received.
However, when I click on one of these buttons, I get 10 as an output every time.
for(int i = 0; i < roleButtons.Length; ++i) {
roleButtons[i].onClick.AddListener(delegate { changeEquipment(i); });
}
void changeEquipment(int index) {
Debug.Log(index); //Here I got 10 as an output after every single click.
}
This question already has answers here:
Which is the correct C# infinite loop, for (;;) or while (true)? [closed]
(20 answers)
Closed 2 years ago.
I find empty "for(;;)" statement in someone else code, and I can't figure out why it is used like this.
try
{
for (;;)
{
It is an infinite loop, just like: while (true)
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I am building a small program just kind of to apply some of what I've learned and see if I can push my limits to learn more. I have so far created a Character class and used a constructor to create a "character" named Dick. I have also created a number of variables and methods that when called, increment or decrement certain variables and output certain activities that Dick is involved in on the console screen. My question is whether there is a way to track time while the program is running so that I can set the time when the program is started and then keep track of it as it runs so it will adjust variables such as hunger, tiredness, time to go to work, time to leave work, and then when those variables hit certain numbers, they will call the methods such as go to work, eat, go to sleep, leave work. Basically, if I could track time some how, I could use every 5 seconds to update the variables, and then the "game" would basically run itself. Any ideas?
Here is how you could do it using the System.Diagnostics namespace:
Stopwatch time = new Stopwatch(); //Create a new Stopwatch
time.Start(); //Start The Timer
Thread.Sleep(5000); //Sleeps The Program For 5 Seconds
System.WriteLine("The Timer Is At: " + time.Elapsed); //Displays What The Timer Is At, Should Be 5 Seconds.
Once you have started your timer you can ignore the Thread.Sleep(5000); part because that was just to show that the timer counts up to 5 seconds as the program is slept for 5 seconds. After starting the timer you can go back and compare the time.Elapsed() part to check if it is a multiple of 5 and if it is then update your variables, like so:
Stopwatch time = new Stopwatch();
if (time.Elapsed % 5 == 0) { //Checks If The Remainder of The Timer When Divided By 5 Is 0.
//Change Variables Or Do Whatever Here
} else {
//Do Whatever Needs To Be Done If Timer Isn't At An Interval Of 5
}
Hope this was of some help.
This question already has answers here:
Listen for key press in .NET console app
(10 answers)
Closed 7 years ago.
C# Console Application - I want it to recognize if a certain key is pressed
This is my first year at high school (10th grade), and I've been learning c# for about a couple of months. I don't know much, but we've recently learned the "if" function, and I was wondering if there's any way (inside a console application, not a windows form) to make a certain key to do something. Please try to make it simple for me because I still don't know much. I think it has something to do with bools (we've learned about bools as well).
Use the following method
ConsoleKeyInfo info = Console.ReadKey();
if (info.Key == ConsoleKey.Escape)
{
// do something when Esc button has pressed
}
else if (info.Key == ConsoleKey.Spacebar)
{
// do something else when Space button has pressed
}
this method will not return until you press some key from the keyboard.
after that, you could check the key which pressed.
This question already has answers here:
Is there a way to count the number of IL instructions executed?
(5 answers)
Closed 7 years ago.
I want to mesure how much instruction are executed in method example.
int a=0;
functionTest();
a=getCountInstruction() // return the number of instruction executed until now.
Their any way to do it using Profiler or some Classes ?
You have to add increase counter when you invoke your method
static int a=0;
functionTest()
{
a++;
//your code
}