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.
Related
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.
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 :-)
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 9 years ago.
Improve this question
I have a C# WinForm application that I need to be refreshed every 5 minutes.
I have looked into Background Worker as well as, a Timer Class.
I was just wondering what is the best way to get main form refreshed so user updates can be displayed without having to close the application and reopening it.
that is called polling , you can use Timer control to do that.
Step 1: You need to Subscribe to the Timer Tick event.
Step 2: Set the Interval property of the Timer to 300000 milliseconds for raising the event for every 5 Minutes.
Step 3: In Tick Event Handler just do whatever you want to perform.
Step 4: you can Call the timer1.Stop() method whenever you want to stop the timer.
Note : if you don't stop the timer it becomes infinite.
if you want to stop the timer you can call timer1.Stop();
System.Windows.Forms.Timer timer1 = new System.Windows.Forms.Timer();
timer1.Interval=300000;//5 minutes
timer1.Tick += new System.EventHandler(timer1_Tick);
timer1.Start();
private void timer1_Tick(object sender, EventArgs e)
{
//do whatever you want
RefreshMyForm();
}
private void RefreshMyForm()
{
//update form with latest Data
}
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.
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");
}
}