How do I refresh a form with a timer? - c#

I use this code to refresh my from it work good for one time after that the timer stopped with me
private void Button1_Click(object sender, EventArgs e)
{
System.Windows.Forms.Timer timer1 = new System.Windows.Forms.Timer();
timer1.Interval = 900000;//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()
{
this.Hide();
Graph1 graph = new Graph1();
graph.Show();
}
i don't know what i miss in this code
it hide the from and didn't open again
start refresh that what i looking form

You need to move the timer deceleration out of the button click and make it "global" to the class. Also, set it up on the Form_Load (make sure you wire up the Form_Load method to your Form_Load event.
Also, your hide logic is a bit faulty. You hide the form, then create a graph (but don't attach it to the Form) then show it. Added some comments below to help you navigate these issues.
private System.Windows.Forms.Timer timer1;
private void Form_Load(object sender, EventArgs e)
{
timer1 = new System.Windows.Forms.Timer();
timer1.Interval = 900000;//5 minutes
timer1.Tick += new System.EventHandler(Timer1_Tick);
}
private void Button1_Click(object sender, EventArgs e)
{
if (!timer1.Enabled)
timer1.Start();
}
private void Timer1_Tick(object sender, EventArgs e)
{
//do whatever you want
RefreshMyForm();
}
private void RefreshMyForm()
{
// Do your data update logic here
this.Refresh();
}

Related

what is the way to close the form and open it again

i try to close the from and open it again with this code bout it didn't close the form i found it in the background and open another one for it
private void Graph_Load(object sender, EventArgs e)
{
System.Windows.Forms.Timer timer1 = new System.Windows.Forms.Timer();
timer1.Interval = 60000;//1 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()
{
this.Close();
Graph1 graph = new Graph1();
graph.Show();
}
start refresh is what i looking for
All you have to do is to change RefreshMyForm() to Refresh(); and clear function RefreshMyForm().
private void Graph_Load(object sender, EventArgs e)
{
{
System.Windows.Forms.Timer timer1 = new System.Windows.Forms.Timer();
label1.Text = DateTime.Now.ToString("HH:mm:ss");
timer1.Interval = 60000;//1 minutes
timer1.Tick += new System.EventHandler(Timer1_Tick);
timer1.Start();
}
}
private void Timer1_Tick(object sender, EventArgs e)
{
label1.Text = DateTime.Now.ToString("HH:mm:ss");
Refresh(); // OR Invalidate(); OR Update();
}
Here label1 is simple watch to see how form refreshed every minute

BackgroundWorker never does anything?

This is the code that I am trying to execute, but stepping through my code I never see any progress indicated or updated on my windows form showing progressbar1. This is my 1st attempt in getting a background worker to function properly, and all I have is a windows form with one button on it and this is all of the code involved in the project.
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
private int i = 0;
public Form1()
{
InitializeComponent();
backgroundWorker1.WorkerReportsProgress = true;
backgroundWorker1.WorkerSupportsCancellation = false;
backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged);
backgroundWorker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker1_RunWorkerCompleted);
}
private void button1_Click(object sender, EventArgs e)
{
backgroundWorker1.RunWorkerAsync();
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = sender as BackgroundWorker;
ReadySteadyGo();
worker.ReportProgress((i * 10));
FinalizeAndFinish();
worker.ReportProgress((i * 10));
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
progressBar1.Text = "Done!";
}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
progressBar1.Text = (e.ProgressPercentage.ToString() + "%");
}
private void ReadySteadyGo()
{
Thread.Sleep(100000);
}
private void FinalizeAndFinish()
{
Thread.Sleep(1000);
}
}
It appears that you are using Thread.Sleep() to simulate a long-running operation. There are a few things you should consider based on your code example:
When the backgroundWorker1.RunWorkerAsync(); is executed, it starts working on another thread. Thus, if you are debugging interactively and you have not set a breakpoint in the backgroundWorker1_DoWork method, you are not likely to see this code execute.
When the Thread.Sleep(100000) executes, it essentially means that the background worker will pause for 100 seconds - so you need to make sure you are waiting at least that long to see the UI updated.
Also, as per Hans Passant's comment, consider the following:
Nor can you see it doing anything, there's no point to assigning the
ProgressBar.Text property since it doesn't display text. Set Value
instead.
I recreated your example in Visual Studio and am hitting a breakpoint in backgroundWorker1_DoWork so the multi-threading is working properly, you just need to do proper processing?
private void button1_Click(object sender, EventArgs e)
{
backgroundWorker1.RunWorkerAsync();
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
for (int i = 1; i <= 100; i++)
{
backgroundWorker1.ReportProgress(i);
Thread.Sleep(100);
}
}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
progressBar1.Value = e.ProgressPercentage;
}

dll: timer not firing

I have a dll that has a timer control in it, inside I have a message box. The timer has been enabled and the interval has been set to 100 seconds, but for some reason it's not firing. I added button to check if it's enabled, and timer1.enabled property is set to true, but it doesn't fire even once. Any ideas what could be wrong? Thanks!
Dll Code:
private void timer1_Tick(object sender, EventArgs e)
{
MessageBox.Show("Test");
}
This is how I call the dll form:
M.ModuleInterface module = Activator.CreateInstance(t) as M.ModuleInterface;
Thread t = new Thread(module.showForm);
t.Start();
showForm Method:
void M.ModuleInterface.showForm()
{
log("GUI::Initialized()");
frm.ShowDialog();
}
i believe, judging by your words alone, that you simply forgot to register to the time.
do:
public Form1()
{
InitializeComponent();
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
}
private void timer1_Tick(object sender, EventArgs e)
{
// Your code here
}
this little example works just fine:
private System.Windows.Forms.Timer timer1;
public Form1()
{
InitializeComponent();
timer1 = new System.Windows.Forms.Timer();
timer1.Interval = 100;
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
timer1.Enabled = true;
}
private void Form1_Load(object sender, EventArgs e)
{
// timer is triggered. code here is called
}

C# how to show message after specific time period

In my project I would like to show message or call methods after 5 minutes for example, If the users didn't click on specific button, I wrote this code
Boolean flage = false;
private void button1_Click(object sender, EventArgs e)
{
Timer Clock;
Clock = new System.Windows.Forms.Timer();
Clock.Interval = 1000;
Clock.Start();
Clock.Tick += new EventHandler(Timer_Tick);
}
public void Timer_Tick(object sender, EventArgs eArgs)
{
if (flage == false)
{
MessageBox.Show("after period of time ");
}
}
private void button2_Click(object sender, EventArgs e)
{
flage = true;
}
Its keeping show the messageBox can any body help me.
Your Timer Clock variable is on the stack and ceases to exist when the function exits.
Try making it a member of the class.

Moving (change location) object to up while mousedown on button

I want to change the radiobutton location and make it move up while i am clicking button
tried this
private void up_MouseDown(object sender, MouseEventArgs e)
{
while(P.Location.Y>0)
P.Location = new System.Drawing.Point(P.Location.X, P.Location.Y - 1);
}
P is a radiobutton
I want it to keep moving up while I'm pressing, but it's just jumping up to the up of the form.
it's working good in debugging but it's really moving fast
I want to slow the movement of the radiobutton and make it visible
Actually you are starting a while loop that will not exit until your RadioButton is at the top of your Form wether you are still pressing the Button or not. You can slow it down by putting a Thread.Sleep in your loop that way it is slowed down visible.
private void up_MouseDown(object sender, MouseEventArgs e)
{
while (P.Location.Y > 0)
{
P.Location = new System.Drawing.Point(P.Location.X, P.Location.Y - 1);
System.Threading.Thread.Sleep(10);
}
}
If you want to have better control I would use a Timer. In this example the Interval is set to 10.
private void up_MouseDown(object sender, MouseEventArgs e)
{
timer1.Start();
}
private void up_MouseUp(object sender, MouseEventArgs e)
{
timer1.Stop();
}
private void timer1_Tick(object sender, EventArgs e)
{
if (P.Location.Y > 0)
{
P.Location = new System.Drawing.Point(P.Location.X, P.Location.Y - 1);
}
}
You can use timer. Add a timer from the toolbox, say its name was timer1, then add following method:
private void P_MouseUp(object sender, MouseEventArgs e) {
timer1.Enabled=false;
}
private void P_MouseDown(object sender, MouseEventArgs e) {
timer1.Enabled=true;
}
private void timer1_Tick(object sender, EventArgs e) {
if(P.Location.Y>0)
P.Location=new System.Drawing.Point(P.Location.X, P.Location.Y-1);
}
You can change the interval of timer1 in properties window. I guess you write this for fun; so, have fun!

Categories