How to show countdown on windows form after the media end - c#

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();
}
}

Related

Timer starts when computer is inactive

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());
}

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 stop timer if a Listbox is empty or items are less than 0 in winform applications

I am making a windows app.
A button1 which gets the items in listBox1 from server at the start.
A button2 which starts the timer1.
A timer1 which removes items from listBox1 .
A progressBar1 which shows the progress of this process.
Here is the code:
private void button1_Click(object sender, EventArgs e)
{
jabber.Send("<iq type='get' to='" + textBox1.Text + "#conference.jabber.com' id='qip_1026'><query xmlns='http://jabber.org/protocol/muc#admin'><item affiliation='outcast' /></query></iq>");
}
private void button2_Click(object sender, EventArgs e)
{
progressBar1.Maximum = listBox1.Items.Count;
timer1.Start();
timer1.Interval = 4000;
}
private void timer1_Tick(object sender, EventArgs e)
{
if (listBox1.Items.Count > 0)
{
jabber.Send("<iq type='set' to='" + textBox7.Text + "#conference.jabber.com'><query xmlns='http://jabber.org/protocol/muc#admin'><item jid='" + listBox1.Items[0].ToString() + "' affiliation='none'/></query></iq>");
listBox1.Items.RemoveAt(0);
progressBar1.Value += 1;
label.Text = listBox1.Items.Count.ToString();
}
else
{
timer1.Enabled = False;
}
}
The above code works well till there is one item left in listBox1.
The error is:
System.ArgumentOutOfRangeException was unhandled
Message=InvalidArgument=Value of '0' is not valid for 'index'.
Parameter name: index
It raises an error when listBox1 reaches 0. I want to stop the timer when listbox1 is empty or gets no items or 0 items.
The problem is in this code:
private void timer1_Tick(object sender, EventArgs e)
{
if (listBox1.Items.Count > 0)
{
jabber.Send("<iq type='set' to='" + textBox7.Text + "#conference.jabber.com'><query xmlns='http://jabber.org/protocol/muc#admin'><item jid='" + listBox1.Items[0].ToString() + "' affiliation='none'/></query></iq>");
listBox1.Items.RemoveAt(0);
progressBar1.Value += 1;
label.Text = listBox1.Items.Count.ToString();
}
else
{
timer1.Enabled = False;
}
}
So what is happening is that you are using count to check >0 then calling jabber to do the work, It the call becomes slow- you will see multiple timers getting fired back. So a big queue will be collected there. You need to modify the code a bit here using lock to hold the list and allow jabber to do its work:
private void timer1_Tick(object sender, EventArgs e)
{
lock (listBox1)
{
if (listBox1.Items.Count > 0)
{
jabber.Send("<iq type='set' to='" + textBox7.Text +
"#conference.jabber.com'><query xmlns='http://jabber.org/protocol/muc#admin'><item jid='" +
listBox1.Items[0].ToString() + "' affiliation='none'/></query></iq>");
listBox1.Items.RemoveAt(0);
progressBar1.Value += 1;
label.Text = listBox1.Items.Count.ToString();
}
else
{
timer1.Enabled = False;
}
}
}
Lock will also ensure that the items are removed correctly.
To save the file as per comment below :
public class ChatHistoryManager
{
private readonly RichTextBox richTextBox;
private Timer timer = new Timer();
public ChatHistoryManager(RichTextBox richTextBox)
{
this.richTextBox = richTextBox;
this.InitializeTimer();
}
public string Location { get; set; }
private void InitializeTimer()
{
this.timer.Tick += timer_Tick;
this.timer.Enabled = true;
this.timer.Interval = (int) TimeSpan.FromHours(1).TotalMilliseconds;
}
void timer_Tick(object sender, EventArgs e)
{
this.SaveFile();
}
public void SaveFile()
{
//Save the file to the location
this.richTextBox.SaveFile(this.Location,RichTextBoxStreamType.RichText);
}
public void Stop()
{
this.timer.Stop();
}
}
Now we need to set in form like this:
private void Form1_Load(object sender, EventArgs e)
{
ChatHistoryManager chatHistoryManager = new ChatHistoryManager(this.richTextBox1);
chatHistoryManager.Location = #"C:\Development\test.txt";
}
try this
int count = City.Items.Count - 1;
for (int i = count; i > 0; i--){
City.Items.RemoveAt(i);
}
Here is what worked for me.
private void button1_Click(object sender, EventArgs e)
{
jabber.Send("<iq type="get" to="" + textBox1.Text + "#conference.jabber.com" id="qip_1026"><query xmlns="http://jabber.org/protocol/muc#admin"><item affiliation="outcast" /></query></iq>");
}
private void button2_Click(object sender, EventArgs e)
{
progressBar1.Maximum = listBox1.Items.Count;
progressBar1.Value = 0;
// Set the timer interval to four seconds
timer1.Interval = 4000;
// Start the timer
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
// Disable the timer while we are working in this procedure so
// it doesn't tick while we are working in this procedure
timer1.Enabled = False;
// Send only if there are items in the ListBox
if (listBox1.Items.Count > 0)
{
jabber.Send("<iq type="set" to="" + textBox7.Text + "#conference.jabber.com"><query xmlns="http://jabber.org/protocol/muc#admin"><item jid="" + listBox1.Items[0].ToString() + "" affiliation="none" /></query></iq>");
listBox1.Items.RemoveAt(0);
progressBar1.Value += 1;
label.Text = listBox1.Items.Count.ToString();
}
// Re-enable only if there are items left in the ListBox
if (listBox1.Items.Count > 0)
{
timer1.Enabled = True;
}
}

Timer.Start() works only on first call?

I'm using System.Windows.Forms.Timer to display a transfer file update progress(timeleft, speed ..etc)
and I also use backgroundworker to send the file
backgroundWorker1_DoWork calls timer1.Start();
backgroundWorker1_RunWorkerCompleted calls timer1.Stop();
It works fine only in the first call for timer1.Strat, but when it called again after timer1.Stop(). It doesn't work.
timer1.Enabled = True;
timer1.Interval = 1000;
private void timer1_Tick(object sender, EventArgs e)
{
long speed = sumAll - prevSum;
prevSum = sumAll;
labelSpeed.Text = CnvrtUnit(speed) + "/S";
if (speed > 0)
{
totalSeconds++;
labelTime.Text = FormatRemainingText(TimeSpan.FromSeconds((sizeAll - sumAll) / speed));
labelTotalTime.Text = FormatRemainingText(TimeSpan.FromSeconds(totalSeconds));
}
}
What's wrong with it and how do I fix it?
I've figured it out, I use System.Timers.Timer instead of System.Windows.Forms.Timer
System.Timers.Timer timer1 = new System.Timers.Timer(1000);
In class constructor I added:
public FileTransfer()
{
InitializeComponent();
timer1.Elapsed += timer1_Tick;
}
private void timer1_Tick(object sender, EventArgs e)
{
long speed = sumAll - prevSum;
Console.WriteLine(speed);
prevSum = sumAll;
Speed(CnvrtUnit(speed) + "/S");
if (speed > 0)
{
totalSeconds++;
Timeleft(FormatRemainingText(TimeSpan.FromSeconds((sizeAll - sumAll) / speed)));
TotalTime(FormatRemainingText(TimeSpan.FromSeconds(totalSeconds)));
}
}
private void Timeleft(string value)
{
if (InvokeRequired)
{
this.Invoke(new Action<string>(Timeleft), new object[] { value });
return;
}
labelTime.Text = value;
}
private void TotalTime(string value)
{
if (InvokeRequired)
{
this.Invoke(new Action<string>(TotalTime), new object[] { value });
return;
}
labelTotalTime.Text = value;
}
private void Speed(string value)
{
if (InvokeRequired)
{
this.Invoke(new Action<string>(Speed), new object[] { value });
return;
}
labelSpeed.Text = value;
}
Now it works every time I call timer1.Start(), no need to "AutoReset".

Categories