Timer starts when computer is inactive - c#

What code should I write to achieve this: IDLE Timer that will start after 15 minutes of no movement in the computer and the IDLE timer will stop if you do movement in the computer again.
Additionally, I want a timer (Activity timer) to stop when the IDLE timer starts. Then the Activity timer resumes when there is movement in the pc again and the IDLE timer stops.
This is what I have done so far:
namespace TITOMS_LOGIN
{
public partial class Form3Admin: Form
{
int seconds;
int minutes;
int hours;
public Form3Admin()
{
InitializeComponent();
seconds = minutes = hours = 0;
}
private void button1_Click(object sender, EventArgs e)
{
this.Hide();
Form2Admin sw = new Form2Admin();
sw.Show();
}
private void button3_Click(object sender, EventArgs e)
{
this.Hide();
Form5 se = new Form5();
se.Show();
}
private void Form3Admin_Load(object sender, EventArgs e)
{
Time.Text = DateTime.Now.ToShortTimeString();
Day.Text = DateTime.Now.ToLongDateString();
}
private void timer1_Tick(object sender, EventArgs e)
{
seconds++;
if (seconds > 59)
{
minutes++;
seconds = 0;
}
if (minutes > 58)
{
hours++;
minutes = 0;
}
lblhours.Text = hours.ToString() + "HRS";
lblminutes.Text = minutes.ToString() + "MINS";
}
private void button4_Click(object sender, EventArgs e)
{
timer1.Start();
}
private void button5_Click(object sender, EventArgs e)
{
timer1.Stop();
}
}
}

private void timer3_Tick(object sender, EventArgs e)
{
TimeSpan idleTime = TimeSpan.FromMinutes(0.5);
TimeSpan aa = TimeSpan.FromSeconds(1);
if (UserInput.IdleTime >= idleTime && timer1.Enabled)
{
timer2.Start();
timer1.Stop();
Console.WriteLine("Stopped Timer 1, Start Timer 2 ");
}
else if (UserInput.IdleTime < aa && timer2.Enabled)
{
timer1.Start();
timer2.Stop();
Console.WriteLine("Stopped Timer 2, Start Timer 1 ");
}
Console.WriteLine("Idle for " + UserInput.IdleTime.ToString());
}

Related

so I was trying to make a code that changes the writings font(arial,times new roman etc.) in the textbox every 750 miliseconds but I couldn't do it

private void OnTimer(object sender, EventArgs e)
{
int i = 0;
i += 1;
}
I tried to make the timer
private void Form1_Load(object sender, EventArgs e)
{
int i = 0;
FontFamily[] fontlar = FontFamily.Families;
fontlar.GetUpperBound(i);
Timer timer1 = new Timer
{
Interval = 750
};
timer1.Enabled = true;
timer1.Tick += new System.EventHandler(OnTimer);
}
I put the code in form1 but I tried it in textbox as well it didn't work
private void button1_Click(object sender, EventArgs e)
{
timer1.Enabled = true;
}
button
I guess you are new to this.
Your Timer does nothing
Your Timer is local, it should be public
You didn't create your Textbox
I think this would be your answer:
Timer timer1;
int i = 0;
private void Form1_Load(object sender, EventArgs e)
{
timer1 = new Timer
{
Interval = 750
};
timer1.Enabled = true;
timer1.Tick += new System.EventHandler(OnTimer);
}
private void OnTimer(object sender, EventArgs e)
{
i += 1;
FontFamily[] fontlar = FontFamily.Families;
//fontlar.GetUpperBound(i);
textBox1.Font = new Font(fontlar[i], 16.0f);
}
private void button1_Click(object sender, EventArgs e)
{
timer1.Enabled = true;
}
Don't forget to create a textbox on your form.

Auto Switch between multiple forms with timer

I have 5 Forms, 1 main form, and 4 forms I want them to switch between each other Automatically every couple of seconds (take turns, each form x seconds and switch to the next).
I have 2 forms so far switching between each other every 2 seconds.
void mytimer_Tick(object sender, EventArgs e)
{
if (!frm2.Focused)
frm2.Focus();
else
frm3.Focus();
}
private void Form1_Load_1(object sender, EventArgs e)
{
Timer mytimer = new Timer();
mytimer.Tick += mytimer_Tick;
mytimer.Interval = 2000;
mytimer.Start();
}
Thankyou.
Crude format. But you will get the idea.
private void HideAllForms()
{
frm1.Hide();
frm2.Hide();
frm3.Hide();
frm4.Hide();
}
void mytimer_Tick(object sender, EventArgs e)
{
if (frmSrl == 1)
{
frmSrl++;
HideAllForms();
frm1.Show();
}
else if (frmSrl == 2)
{
frmSrl++;
HideAllForms();
frm2.Show();
}
else if (frmSrl == 3)
{
frmSrl++;
HideAllForms();
frm3.Show();
}
else if (frmSrl == 4)
{
frmSrl =1;
HideAllForms();
frm4.Show();
}
else
frmSrl = 1;
}
int frmSrl = 1;
private void Form1_Load_1(object sender, EventArgs e)
{
Timer mytimer = new Timer();
mytimer.Tick += mytimer_Tick;
mytimer.Interval = 2000;
mytimer.Start();
}

How to run a progress bar on a timer - c#

I want to run a progress bar on a form through the use of a timer.
I have tried multiple ways and have not been able to get it to work.
I hope someone here can help me with this.
private void SplashScreen_Load(object sender, EventArgs e)
{
splashScreenTimer.Enabled = true;
splashScreenTimer.Start();
splashScreenTimer.Interval = 1000;
progressBar.Maximum = 100;
splashScreenTimer.Tick += new EventHandler(timer1_Tick);
}
private void timer_Tick(object sender, EventArgs e)
{
if (progressBar.Value != 10)
{
progressBar.Value++;
}
else
{
splashScreenTimer.Stop();
}
}
you are assigning event_handler like
splashScreenTimer.Tick += new EventHandler(timer1_Tick);
and you are changing the progressBar value in
private void timer_Tick(object sender, EventArgs e)
{
if (progressBar.Value != 10)
{
progressBar.Value++;
}
else
{
splashScreenTimer.Stop();
}
}
change event handler to
splashScreenTimer.Tick += new EventHandler(timer_Tick);
or move codes to the other event handler timer1_Tick which should be in your form
For running the progressBar full in 4 seconds you can do like this
private void Form1_Load(object sender, EventArgs e)
{
splashScreenTimer.Enabled = true;
splashScreenTimer.Start();
splashScreenTimer.Interval = 30;
progressBar.Maximum = 100;
splashScreenTimer.Tick += new EventHandler(timer_Tick);
}
int waitingTime = 0;
private void timer_Tick(object sender, EventArgs e)
{
if (progressBar.Value < 100)
{
progressBar.Value++;
}
else
{
if (waitingTime++ > 35)
this.Close();
}
}

How to show the label for couple of seconds and show another label in form

I have a mini form3 https://imageshack.com/i/p1zxB6Lqp which shows a gif image running for 4 sec. So i need to show 4 different
labels in same position..
For example
label 1 - `Connecting to smtp server..`
label 2 - `Fetching recipients..`
label 3 - `Attaching necessary G-code files..`
label 4 - `Please wait sending..`
How can i show all these labels one after another in same position.. so it looking more professional
for sending mail.
My code snippet:-
Form1
private void button2_Click(object sender, EventArgs e)
{
//mail inforamtion
_f3.ShowDialog(); // - - >> is the form i wanted with all labels
smtp.Send(msg);
MessageBox.Show("Email Successfully Sent!!!", "Mail!!!.");
Environment.Exit(0);
}
Form3:
Timer formCloser = new Timer();
private void Form3_Load(object sender, EventArgs e)
{
timer1.Interval = 5000;
timer1.Enabled = true;
timer1.Tick += new EventHandler(timer1_Tick);
}
private void timer1_Tick(object sender, EventArgs e)
{
this.DialogResult = DialogResult.OK;
timer1.Stop();
}
Please help me out.. how can i add label in my form..
Found the answer.. I kept two timers for each function,
timer1 to run the mini form only for 5 seconds.
and timer two to run only for 1 sec. If any body has a better code to share with please..
Most welcome!!
Here is my code:
private void Form3_Load(object sender, EventArgs e)
{
timer1.Interval = 5000;
timer1.Enabled = true;
timer1.Tick += new EventHandler(timer1_Tick);
timer2.Interval = 1000;
timer2.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
this.DialogResult = DialogResult.OK;
timer1.Stop();
}
int StopTime = 0;
private void timer2_Tick(object sender, EventArgs e)
{
StopTime++;
if (StopTime == 1)
{
label1.Text = " Connecting to smtp server..";
}
if (StopTime == 2)
{
label1.Text = " Fetching recipients..";
}
if (StopTime == 3)
{
label1.Text = " Attaching G-code files..";
}
if (StopTime == 4)
{
label1.Text = " Done!!";
StopTime = 0;
timer2.Stop();
}
}

How to show countdown on windows form after the media end

I am using a listBox to play media files with the axWindowsMediaPlayer on form in which the next media plays after a time gap for which I have used a timer.Now I want to display a coundown timer on screen for that time gap.How to do this..plz help.The code how I am using the timer at media end is:
System.Timers.Timer _timer = new System.Timers.Timer();
object _locker = new object();
Player.PlayStateChange += Player_PlayStateChange;
_timer.Elapsed += _timer_Elapsed;
_timer.Interval = 3000;
void _timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
_timer.Stop();
lock (_locker)
{
this.Invoke((MethodInvoker)delegate
{
if (listBox2.SelectedIndex + 1 < listBox2.Items.Count)
{
listBox2.SelectedItem = listBox2.Items[listBox2.SelectedIndex + 1];
}
});
}
}
void Player_PlayStateChange(object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)
{
if ((WMPLib.WMPPlayState)e.newState == WMPLib.WMPPlayState.wmppsMediaEnded)
{
_timer.Start();
}
else if ((WMPLib.WMPPlayState)e.newState == WMPLib.WMPPlayState.wmppsReady)
{
Player.Ctlcontrols.play();
}
}
Just put in _timer_Elapsed() a counter
private int m_counter=0;
make it 0 each for media(lets say after media has started)
and create a Label to show the counter.ToString()
private int totalwaittime = 5;
private int counter = 0;
private static void OnTimer(object sender, ElapsedEventArgs elapsedEventArgs)
{
counter++;
Label1.text = String.Format("Time Left : {0}", totalwaittime - counter);
if (totalwaittime - counter == 0)
{
timer.Enabled = false;
//play next file
}
}
void Player_PlayStateChange(object sender, AxWMPLib._WMPOCXEvents_PlayStateChangeEvent e)
{
if ((WMPLib.WMPPlayState)e.newState == WMPLib.WMPPlayState.wmppsMediaEnded)
{
counter = 0;
timer.Enabled = true;
}
else if ((WMPLib.WMPPlayState)e.newState == WMPLib.WMPPlayState.wmppsReady)
{
Player.Ctlcontrols.play();
}
}

Categories