How to make a timer that reload your consoleapplication C#? [closed] - c#

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm a beginner in coding C#, and for my first program I want to have a timer that reloads my Console Application every 10 seconds. Is it possible? Can you write an example please?

Instead of "reloading" the application, you'd typically have a TImer trigger a specific routine within your application on a regular interval.
class Program
{
static void Main()
{
Console.WriteLine("Starting timer");
var timer = new System.Timers.Timer(10000);
// Hook up the Elapsed event for the timer.
timer.Elapsed += OnTimer;;
timer.Enabled = true;
Console.WriteLine("Press any key to shut down");
Console.ReadKey();
}
static void OnTimer(object source, ElapsedEventArgs e)
{
// This code will run every 10 seconds
Console.WriteLine("In timer");
}
}

Related

Unable to change text of textblock in Windows Phone 8.0 [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I'm trying to set the value of a textblock to what hour it is, I have the following code:
timeHours.Text = DateTime.Now.Hour.ToString();
But the textblock doesn't show what hour it is, it doesn't show anything and stays as the original text, how do I solve this problem?
If you want to show the current hour in a text box, and have it update properly every 60 minutes then you need some sort of background process that changes the value that is displayed in the textbox. You can do this with a simple Timer object and an event.
using System.Timers;
class myclass
{
System.Timers.Timer timer;
public void initialise()
{
timer = new System.Timers.Timer(10000);
timer += new ElapsedEventHandler(_timer_Elapsed);
}
void _timer_Elapsed(object sender, ElapsedEventArgs e)
{
timeHours.Text = DateTime.Now.Hour.ToString();
}
}
This updates the hour every 10 seconds (10000 milliseconds), not very efficient but does the job.

Windows Form C# Button blink On and Off [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I have this game in Windows Form that displays 9 buttons on the screen, you have to guess the number the computer thinks, if the number is correct the button should blink/flash on and off. I looked out but i couldn't find any relevant function to help me out. If someone can help me I would really appreciate. Thank you.
You can use the Timer class, which switches the background color of your buttons on the
Elapsed event.
Sample code:
// Create a timer with a ten second interval.
System.Timers.Timer aTimer = new System.Timers.Timer(10000);
// Hook up the Elapsed event for the timer.
aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
// Set the Interval to 2 seconds (2000 milliseconds).
aTimer.Interval = 2000;
aTimer.Enabled = true;
The OnTimeEvent function:
void OnTimedEvent(object source, ElapsedEventArgs e)
{
// Change button color
}
Hope it helps :-)

I cannot use the form which I've opened from Timers.Timer event [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I am a beginner in C#.
My idea is to create an application which is checking remote web site and show some form if condition is true. So it checks every 10 second, it should works like a notifier.
I start a timer when start my application.
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
TimerEvent timer = new TimerEvent();
timer.startTimer();
Application.Run();
}
}
Then I open the form every 10 second.
class TimerEvent
{
public void startTimer()
{
System.Timers.Timer aTimer = new System.Timers.Timer();
aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
aTimer.Interval = 10000;
aTimer.Enabled = true;
}
private void OnTimedEvent(object source, ElapsedEventArgs e)
{
Form1 form = new Form1();
form.Show();
}
}
It opens every 10 second new form, but I cannot use it.
I assuming that I missed something about threading.
Thanks in advance.
UPDATE
Solution for me C# open a new form, and close a form
You are using a System.Timers.Timer without using the SynchronizingObject property. That means you are calling Show() from a pool thread, it should not work at all (in Debug mode).
Also you are calling Application.Run() without specifying a MainForm.
The solution dpends on what you actually want to happen (a new Form per 10 seconds gets pretty messy quickly).
Use a normal MainForm and a Windows.Forms.Timer to pop up new Forms.
I assuming that I missed something about threading.
The main thing to know is that the UI is single-threaded by design. Use events, not threads.

Debug.WriteLine doesn't work on different threads [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
var timer = new Timer
{
Enabled = true,
Interval = 500
};
Debug.WriteLine("new timer created");
timer.Elapsed += (s, e) =>
{
Debug.WriteLine("hello from timer");
// this doesn't get shown
// in the output window
}
What do I need to do to be able to see my last Debug.WriteLine() in the output window above?
You are actually not using separate threads, the System.Timers timer runs on the UI Message pump if it is available. If you have blocked the UI thread the timer will never run.
Check either that the UI thread is not blocked, use System.Threading.Timer that does not use the UI thread, or set SynchronizingObject to null so it will use a threadpool thread instead of the UI thread.
EDIT: Or like Hans said in his comment, you are running in the console and Main() is exiting before the timer has a chance to fire.

Displaying a message box after a certain period of time [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Hie guys, i have no code at the moment but my problem is like this. i want to display a message box after a certain period of time after an event has occurred lets say after an enquiry has been made and this message box seeks to remind the user to check on the delivery status of the enquiry lets say after 5 minutes. Any ideas or alternatives?
First create a function to show your message, then you can use a timer.tick event to show your message at a given interval
static System.Windows.Threading.DispatcherTimer myTimer = new System.Windows.Threading.DispatcherTimer();
public void DoInquiry()
{
// do your inquiry stuff
////////////////////////
// Set Timer Interval
myTimer.Interval = = new TimeSpan(0,5,0); // 5 Minutes
// Set Timer Event
myTimer.Tick += new EventHandler(TimerEventProcessor);
// Start timer
myTimer.Start();
}
private static void TimerEventProcessor(Object myObject, EventArgs myEventArgs) {
ShowMessage("Please check on the status");
}
protected void ShowMessage(string Message)
{
System.Windows.MessageBox.Show(Message);
}
You would probably want to use a DispatcherTimer. You can use that to check when five minutes have passed then show a MessageBox when appropriate.

Categories