How to invoke a method based on time [closed] - c#

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Hi there I am fairly new to programming in C#. How can I make use of a time stamp to control the invocation of a method?
For example, if the time stamp is under a certain number of seconds, invoke one method.
If it's over a certain period of time run, invoke another method.
Can anyone steer me in the right direction?
This is the code I'd like to use:
DateTime.Now.Second + DateTime.Now.Minute * 60;
I know how to use if and else statements. All I want to do is make the computer control when they are activated using time.

You would want to use if-else statement
// Sample usage
var seconds = DateTime.Now.Second;
var time = DateTime.Now.Second + DateTime.Now.Minute * 60;
if(time < certainNumberOfSeconds) // Sample Condition.
// Invoke a method
else
// Invoke a different method.

var secondsUntilRunSecondMethod = (WaitingInSeconds - currentTime.Seconds) % 60;
You can use secondsUntilRunSecondMethod in a timer and run the method form the timer.

Related

For loop does not finish [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 11 months ago.
Improve this question
I am making a game in Unity, 2D and for the creation of levels, I have added a for loop so that a number of blocks is generated a certain number of times. The problem is that it never ends, that is, instead of ending on the game screen when the two blocks are created, it continues to generate blocks infinitely.
public void GenerateInitialBlocks()
{
for (int i = 0; i < 2; i++)
{
AddLevelBlock();
}
}
I have reset the Script in Unity because it usually gives compilation errors or crashes, but it still doesn't work. Thanks for read.
I believe the problem could be in how many times the method is called.
find where in the code the method is called (perhaps using search)
make sure it's called only once per level and not repeatedly when refreshing or repainting (scene updating methods).

nth root of a number based on two user inputs [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
var result6 = Math.Sqrt(num1, num2);
Console.Write(name + " this is the final result of square rooting = "+ result6);
I would like to find the nth root of a number based on two user inputs. I am trying to use the Math.Sqrt() method to achieve this.
This piece of script is outputting an error No overload for method 'Sqrt' takes 2 arguments [Main2], is there a method to fix this error?
As stated in the docs the Math.sqrt(double) method only takes one parameter, no overloads.
If you meant to take the nth root, you could use Math.Pow(Double, Double) and put 1 over the second parameter, such as
Math.Pow(64, 1/3); // Cube root of 64
// Output: 4

How many Tasks are too many [closed]

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 working on a spaceship shooter style game. In the game there are slow moving missiles that I run on different tasks.
Task t4 = new Task(() => fireShipCannons());
They don't do much; just move a small bitmap across the screen. Usually it is a for loop with 30 iterations. The problem is that the timing is off and some tasked seem to take priority over others. I also many be important to note that to control the speed of the missiles I am using Thread.Sleep(100). Because of this slowing down, the method will run for a few seconds.
I am using c#, .net, Windows Form Application
I have spent hours thinking about how to reword this, and I think it is worded correctly. The problem I was having was broad; so yes, the question may be a bit broad. I got a great answer! Does that not indicate that the question was asked correctly?
Since you are already using Tasks (good), you can simply make them async Tasks and use await Task.Delay(time).
async Task fireShipCannons(CancellationToken ct)
{
for (int i = 0; i < distance; i++) {
await Task.Delay(100, ct);
drawBitmap(i);
}
}
This way you will not tie up 1 thread for every bullet. Most likely your tasks are currently slow because you reach the limit of your threadpool, so this should be a lot faster. But additionally, you should also check if Task.Delay has taken more than the 100 ms you suggested. If it did, draw the bullet 2 spots further instead of 1, and reschedule accordingly.
async Task fireShipCannons(CancellationToken ct)
{
var start = DateTime.Now;
while (bullet.isOnscreen) {
drawBitmap(bullet);
var sleep = (start.Add(TimeSpan.FromMilliseconds(100.0)) - DateTime.Now);
if (sleep.Milliseconds > 0) {
Task.Delay(sleep, ct);
}
iter = (int)((DateTime.Now - start).TotalMilliseconds / 100 + 0.5);
bullet.moveSteps(iter); // move it iter steps.
var start = start.AddMilliseconds(100 * it);
}
}
Lastly, it might be a good idea to not hammer your draw function with new calls for every bullet, but instead combine all bullets/changes into a single render function that calculate all changes and sends them off in one go. But that depend on how your draw function itself is implemented.
It depends on the machine and the number of threads it can support. On the other hand, I don't think that using Thread.Sleep it is a good practice, why don't you just change the distance, in pixels, that the object travels every millisecond?

using Time in C# console program [closed]

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.

I want the user to take 1 hour and 10 minutes to give a message that the amount of time remaining [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I want the user to take 1 hour and 10 minutes to give a message that the amount of time remaining
this my code
string your = txt2.Text;
string format = "HH:mm:ss";
DateTime your1 = DateTime.Parse(your);
while (DateTime.Now < your1)
{
TimeSpan left = your1 - DateTime.Now;
DateTime left1 = Convert.ToDateTime(left.ToString());
MessageBox.Show(left1.ToString() + "1");
}
MessageBox.Show("times up");
I would suggest you create a form (instead of the MessageBox.Show). Inside this form you can display the remaining time and add a timer that calls "this.close()" after a period of time

Categories

Resources