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".
Related
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();
}
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();
}
}
So I'm trying to make a basic stop watch program but I keep getting this error "A new expression requires (), [], or {} after type"
Here's my Code:
namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
int ms, s, m, h;
Timer timer1 = new Timer
public Form1()
{
InitializeComponent();
ms = 0;
}
private void timer1_Tick(object sender, EventArgs e)
{
ms = ms + 1;
if (ms == 9)
{
ms = 0;
s = s + 1;
lblsecond.Text = s.ToString();
if (s == 59)
{
s = 0;
m = m + 1;
lblmin.Text = m.ToString();
if (m == 59)
{
m = 0;
h = h + 1;
lblhur.Text = h.ToString();
{
lblmsec.Text = ms.ToString();
}
}
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void label1_Click(object sender, EventArgs e)
{
}
private void label1_Click_1(object sender, EventArgs e)
{
}
private void label2_Click(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
timer1.Enabled = true;
}
private void button2_Click(object sender, EventArgs e)
{
timer1.Enabled = true;
}
private void button3_Click(object sender, EventArgs e)
{
ms = 0;
h = 0;
s = 0;
m = 0;
timer1.Enabled = false;
lblhur.Text = "00";
lblmin.Text = "00";
lblmsec.Text = "00";
lblsecond.Text = "00";
}
}
}
Timer timer1 = new Timer();
You need to call the constructor of the class, when you create new class. Read some basics tutorials about C#. You are missing the basics !
This line
Timer timer1 = new Timer();
Can you specify which line is giving you the error? But also it might be this, why do you have {} around this?
lblhur.Text = h.ToString();
{
lblmsec.Text = ms.ToString();
}
Or
Timer timer1 = new Timer
to
Timer timer1 = new Timer();
At the this line
Timer timer1 = new Timer
end of Timer need () and ; ,like this:
Timer timer1 = new Timer();
I am designing a program that depends on monitoring the battery level of the computer.
This is the C# code I am using:
PowerStatus pw = SystemInformation.PowerStatus;
if (pw.BatteryLifeRemaining >= 75)
{
//Do stuff here
}
My failed attempt of the while statement, it uses all the CPU which is undesirable.
int i = 1;
while (i == 1)
{
if (pw.BatteryLifeRemaining >= 75)
{
//Do stuff here
}
}
How do I monitor this constantly with an infinite loop so that when it reaches 75% it will execute some code.
Try Timer:
public class Monitoring
{
System.Windows.Forms.Timer timer1 = new System.Windows.Forms.Timer();
public Monitoring()
{
timer1.Interval = 1000; //Period of Tick
timer1.Tick += timer1_Tick;
}
private void timer1_Tick(object sender, EventArgs e)
{
CheckBatteryStatus();
}
private void CheckBatteryStatus()
{
PowerStatus pw = SystemInformation.PowerStatus;
if (pw.BatteryLifeRemaining >= 75)
{
//Do stuff here
}
}
}
UPDATE:
There is another way to do your task complete. You can use SystemEvents.PowerModeChanged.
Call it and wait for changes, monitor the changes occured then do your stuff.
static void SystemEvents_PowerModeChanged(object sender, Microsoft.Win32.PowerModeChangedEventArgs e)
{
if (e.Mode == Microsoft.Win32.PowerModes.StatusChange)
{
if (pw.BatteryLifeRemaining >= 75)
{
//Do stuff here
}
}
}
While loop will cause your UI to response poor and the application will get crashed. You can solve this by using many ways. Please check out the below code snippet will help your needs.
public delegate void DoAsync();
private void button1_Click(object sender, EventArgs e)
{
DoAsync async = new DoAsync(GetBatteryDetails);
async.BeginInvoke(null, null);
}
public void GetBatteryDetails()
{
int i = 0;
PowerStatus ps = SystemInformation.PowerStatus;
while (true)
{
if (this.InvokeRequired)
this.Invoke(new Action(() => this.Text = ps.BatteryLifePercent.ToString() + i.ToString()));
else
this.Text = ps.BatteryLifePercent.ToString() + i.ToString();
i++;
}
}
BatteryChargeStatus.Text = SystemInformation.PowerStatus.BatteryChargeStatus.ToString();
BatteryFullLifetime.Text = SystemInformation.PowerStatus.BatteryFullLifetime.ToString();
BatteryLifePercent.Text = SystemInformation.PowerStatus.BatteryLifePercent.ToString();
BatteryLifeRemaining.Text = SystemInformation.PowerStatus.BatteryLifeRemaining.ToString();
PowerLineStatus.Text = SystemInformation.PowerStatus.PowerLineStatus.ToString();
If you want to perform some operation just convert these string values into the integer.
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();
}
}