cycle in C#.NET win form - c#

I have C#.NET program and I want to check something every 2 seconds until my form is closed. This cycle doesn't enable with user for other hand when user run my C#.NET form cycle is start until form closed.
For example I want to check internet connection when my form is used. I don't like checking the connection in form.load;. I want to check connection is every time.

this code is worked :
cycle Timer in winForm c#.NET
using Timer = System.Windows.Forms.Timer;
private void Form1_Load(object sender, EventArgs e)
{
Timer MyTimer = new Timer();
MyTimer.Interval = (45 * 60 * 1000); // 45 mins
MyTimer.Tick += new EventHandler(MyTimer_Tick);
MyTimer.Start();
}
private void MyTimer_Tick(object sender, EventArgs e)
{
//Check for internet connection or some other work here
Timer MyTimer = new Timer();
MyTimer.Interval = (45 * 60 * 1000); // 45 mins
MyTimer.Tick += new EventHandler(MyTimer_Tick);
MyTimer.Start();
}

Related

C# TimeOut Method

I have the following code but it is not executing for the TimeOut object for inactivity
private void Form1_Load(object sender, EventArgs e)
{
Console.WriteLine("booting up....");
programController.init(this);
disableFetchSetData();
label3.Hide();
logoutButton.Hide();
//Added Timer Object to log out of program
Timer MyTimer = new Timer();
MyTimer.Interval = (10 * 60 * 1000); // 10 mins
MyTimer.Tick += new EventHandler(TimeOut);
MyTimer.Start();
}
public void TimeOut(object sender, EventArgs e)
{
MessageBox.Show("The form will now close due to inactivity");
this.Close();
}
Why is this? It is supposed to execute from the Load() of the Form. Since the TimeOut method is an event, it is called upon the Load of the Form.

C# How to run delayed method in between timer ticks

I am using a timer to run a method every 16 minutes. I also want to run a second method every minute for 15 minutes in-between.
Below is the code I am using:
int count = 0;
private void cmdGo_Click(object sender, EventArgs e)
{
System.Windows.Forms.Timer t = new System.Windows.Forms.Timer();
t.Interval = 960000; // specify interval time - 16 mins
t.Tick += new EventHandler(timer_Tick);
t.Start();
}
void timer_Tick(object sender, EventArgs e)
{
RunMethod1();
while(count < 15)
{
//waiting for 60 seconds
DateTime wait = DateTime.Now;
do
{
Application.DoEvents();
} while (wait.AddSeconds(60) > DateTime.Now);
RunMethod2();
}
}
The above code seems to work fine but the ‘do while’ loop to wait for 60 seconds is very CPU heavy.
I tried to use Thread.Sleep(60000) but this freezes up the Interface and also tried to add a second timer within timer_Tick but this doesn’t seem possible. Can a second timer be added within the EventHandler of the first?
Is there any other method to achieve this without being so CPU intensive?
Thanks!
Warren
NOTE: Sorry guys, there was a typo in my original post. The 60 second wait do, while loop should have been within the while < 15 loop. Just updated the code snippet.
So:
RunMethod1() should be executed every 16 mins
RunMethod2() should be executed every 1 min (15 times) in between the 16 min tick
It would make more sense to have a counter to store how many times the clock has gone off. Then set your timer interval to fire once a minute so not doing anything in between...
That way you could just do...
private int Counter;
private void cmdGo_Click(object sender, EventArgs e)
{
System.Windows.Forms.Timer t = new System.Windows.Forms.Timer();
t.Interval = 60000; // specify interval time - 1 minute
t.Tick += new EventHandler(timer_Tick);
t.Start();
}
// Every 1 min this timer fires...
void timer_Tick(object sender, EventArgs e)
{
// If it has been 16 minutes then run RunMethod1
if (++Counter >= 16)
{
Counter = 0;
RunMethod1();
return;
}
// Not yet been 16 minutes so just run RunMethod2
RunMethod2();
}
You could await a task Delay so the UI will keep responding
async void timer_Tick(object sender, EventArgs e)
{
RunMethod1();
while (count < 15)
{
//waiting for 60 seconds
await Task.Delay(60000);
RunMethod2();
}
}

Maximize windows every Xth minute

I am developing a C# desktop application. I want my all open windows to pop up (something which happens with Alt + Tab) every 5th minute. I looked at a few questions here. They suggest doing it by using timers, but how do I pop up the minimised windows?
Here is a really basic example for you to work on.
First create the timer.
Create a function that will run when the timer ticks.
Then add an event to run every time it ticks. And link it your function
Inside that function check if it has been 5 minutes. If so, maximize
the window
public partial class TimerForm : Form
{
Timer timer = new Timer();
Label label = new Label();
public TimerForm ()
{
InitializeComponent();
timer.Tick += new EventHandler(timer_Tick); // Everytime timer ticks, timer_Tick will be called
timer.Interval = (1000) * (1); // Timer will tick evert second
timer.Enabled = true; // Enable the timer
timer.Start(); // Start the timer
}
void timer_Tick(object sender, EventArgs e)
{
// HERE you check if five minutes have passed or whatever you like!
// Then you do this on your window.
this.WindowState = FormWindowState.Maximized;
}
}
Here is the complete solution
public partial class Form1 : Form
{
int formCount = 0;
int X = 10;
System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();
public Form1()
{
InitializeComponent();
timer.Tick += new EventHandler(timer_Tick); // Everytime timer ticks, timer_Tick will be called
timer.Interval = (1000) * X; // Timer will tick evert second
timer.Enabled = true; // Enable the timer
timer.Start();
}
void timer_Tick(object sender, EventArgs e)
{
FormCollection fc = new FormCollection();
fc = Application.OpenForms;
foreach (Form Z in fc)
{
X = X + 5;
formCount++;
if (formCount == fc.Count)
X = 5;
Z.TopMost = true;
Z.WindowState = FormWindowState.Normal;
System.Threading.Thread.Sleep(5000);
}
}
}

Running a task in windows service

I am making a windows service and one of its task is to ask for a free disk space every 1 hour, I know how to get the free space when the service starts but how to check for it every 1 hour?
Use a Timer like System.Timers.Timer:
var timer = new System.Timers.Timer();
timer.Elapsed += new ElapsedEventHandler(TimerElapsed);
timer.Interval = 60 * 60 * 1000; // 1 hour
timer.Enabled = true;
...
private static void TimerElapsed(object source, ElapsedEventArgs e)
{
// check disk space
}
Start thread:
while(true){
getFreeSpace();
Sleep(3600*1000);
};

how to use timer effectively in c#

I have to show one form for about 5 sec and then I have to close that form and then show some other form once the new form is shown the timer has to stop.
I have difficulty in doing this.
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
Form5 f5 = new Form5();
f5.Show();
f5.label7.Text = label6.Text;
MyTimer.Interval = 5000; // 0.5 mins
MyTimer.Tick += new EventHandler(MyTimer_Tick);
MyTimer.Start();
}
private void MyTimer_Tick(object sender, EventArgs e)
{
MessageBox.Show("All The Best for Your Test and Your Time Starts Now.");
Form6 f6 = new Form6();
f6.Show();
MyTimer.Enabled = false;
Form5 f5 = new Form5();
f5.Hide();
}
try this code
Form5 f5 = new Form5();
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
f5.Show();
f5.label7.Text = label6.Text;
MyTimer.Interval = 5000; // 0.5 mins
MyTimer.Tick += new EventHandler(MyTimer_Tick);
MyTimer.Start();
}
private void MyTimer_Tick(object sender, EventArgs e)
{
MessageBox.Show("All The Best for Your Test and Your Time Starts Now.");
Form6 f6 = new Form6();
f6.Show();
MyTimer.Enabled = false;
MyTimer.stop();
f5.Hide();
}
Pull the declaration of Form5 outside of the functions, so it is a field. As it stands, each function has a different instance of this class.
Form5 f5 = new Form5();
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
f5.Show();
f5.label7.Text = label6.Text;
MyTimer.Interval = 5000; // 0.5 mins
MyTimer.Tick += new EventHandler(MyTimer_Tick);
MyTimer.Start();
}
private void MyTimer_Tick(object sender, EventArgs e)
{
MessageBox.Show("All The Best for Your Test and Your Time Starts Now.");
Form6 f6 = new Form6();
f6.Show();
MyTimer.Enabled = false;
MyTimer.Stop();
f5.Hide();
}
Note: You really should rename your form classes and the variables - f6 is meaningless. Call it what it is.
try this
Form5 f5 = new Form5(); //declare form obj globally
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
f5.Show();
f5.label7.Text = label6.Text;
MyTimer.Interval = 5000; // 0.5 mins
MyTimer.Tick += new EventHandler(MyTimer_Tick);
MyTimer.Start();
}
private void MyTimer_Tick(object sender, EventArgs e)
{
MessageBox.Show("All The Best for Your Test and Your Time Starts Now.");
Form6 f6 = new Form6();
f6.Show();
MyTimer.Enabled = false;
f5.Hide();
}
I see several potential problems. First, you should setup your timer only once, perhaps at Form construction time, you don't need to set the interval and wire up an even handler every single time radioButton1's check state changes.
Inside of MyTimer_Tick, the first line should be calling MyTimer.Stop() (just call stop, you don't need to mess with Enabled, they do the same thing).
THEN you can display the MessageBox (which is modal and blocking), show Form6, hide f5, etc.
Think of MessageBox.Show() as a really long running call. It doesn't return until the message box is dismissed (it can easily take more than 5 seconds or any arbitrary amount of time). While the MessageBox is up, timer tick events are still queuing up (because the line that stops the timer hasn't been executed yet). It would be worth looking up the documentation for MessageBox.Show() and reading about what a modal dialog is and how its different from the alternative.
And try and clean up the names as other's have pointed out.
I think You're making it to difficult. If I understood You correctly...
Just add a timer to form5, set it's properties Enabled = true; and Interavl = 1000; (1000 miliseconds or 1 second). and just add timer tick event handler to your form5 timer like
private int _start = 0;
private int _seconds = 5;
private void timer1_Tick(object sender, EventArgs e)
{
_start++;
if(_start >= _seconds)
{
Close();
}
}
_start and _seconds should be initialized like form class private fields, or properties before event handler. This code works fine for me, and it closes form5 after 5 seconds it was shown. If you want to make this more flexible, for example if you want to set how many seconds form5 should be shown U can, for example, reload form5 constructor like ...
public Form5(int seconds)
{
InitializeComponent();
_seconds = seconds;
}
and in form1, when you create form5 pass number of seconds U want to show form5 as parameter:
Form5 f5 = new Form5(5);
also I think it would be better to create new instance of form 5 dirrectly in event handler
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
new Form5(10).Show(); // show form5 for 10 seconds
...
}
If U want to show some another more form after form5 close, just show it before form5 close in timer tick event handler:
private void timer1_Tick(object sender, EventArgs e)
{
_start++;
if(_start >= _seconds)
{
new Form2().Show();
Close();
}
}

Categories