Timer to refresh charts in c# - c#

I have created a chart which receives its data from a SQL Server database. Whenever I save new data into the database, all the reports get updated, but the chart does not update until I exit the application and log in again. I decided to use Timer in C# to automatically refresh the chart every 5 seconds.
//this is to invoke the timer as soon as the application launches.
public MDIParent2()
{
InitializeComponent();
myTimer.Enabled = true;
myTimer.Start();
}
//this is my timer event
private void myTimer_Tick(object sender, EventArgs e)
{
this.Refresh();
MessageBox.Show("Refreshed!");//this was added to determine if the timer is working
}
//this lets me stop the timer
private void button1_Click(object sender, EventArgs e)
{
myTimer.Stop();
}
//this lets me resume the timer
private void button2_Click(object sender, EventArgs e)
{
myTimer.Start();
}
I get the message "Refreshed!" every 5 seconds, but chart is still not refreshed. Can someone please assist?

replace this.refresh() with [Chart Object].refresh()

Related

Cancel button wait for BackgroundWorker to finish (ASP.NET C#)

I'm working with C# ASP.NET Web Application. There is a problem that is difficult to track in the code.
Let's say I have two buttons: Upload (which uploads some data from DataTable to Database) and Cancel (which cancels the uploading process). The problem is that uploading process runs in the BackgroundWorker. When user clicks Upload and uploading starts and then he clicks Cancel the Cancel_Button_Click event does not fire until BackgroundWorker is uploading data.
I have code like this:
protected void Page_Load(object sender, EventArgs e)
{
backgroundWorker1.ProgressChanged += new System.ComponentModel.ProgressChangedEventHandler(backgroundWorker1_ProgressChanged);
backgroundWorker1.RunWorkerCompleted += new System.ComponentModel.RunWorkerCompletedEventHandler(backgroundWorker_RunWorkerCompleted);
}
protected void Upload_Button_Click(object sender, EventArgs e)
{ backgroundWorker1.RunWorkerAsync(); }
protected void Cancel_Button_Click(object sender, EventArgs e)
{ backgroundWorker1.CancelAsync(); }
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{ };
So what really happens is when you click Upload and Cancel is method private void backgroundWorker1_ProgressChanged runs many times (one time for every row of data, I suppose) and ONLY THEN method Cancel_Button_Click runs.
What to do, if I want to stop data uploading right after Cancel button is clicked?
Thaaaanks! :)
P.S. I tried searching on the net but I don't even know how to describe my problem in one sentence.

How to setStatus after timer.Elapsed

My question is probably quite simple. In a c# project i am trying to set a status of an instance in a different class at a click event. The trouble is that i want to do this after a certain time has elapsed and without any c# experience I find this quite hard to accomplish.
Thanks in advance!!
my code is as follows:
public void button1_Click(object sender, EventArgs e)
{
kruispunt.zPad1.voetstoplicht1.setStatus(StoplichtStatus.Rood);
kruispunt.zPad1.voetstoplicht2.setStatus(StoplichtStatus.Rood);
this.Refresh();
}
The easiest way is to use async (assuming you're using C# 5):
public async void button1_Click(object sender, EventArgs e)
{
await Task.Delay(Timespan.FromSeconds(5));
kruispunt.zPad1.voetstoplicht1.setStatus(StoplichtStatus.Rood);
kruispunt.zPad1.voetstoplicht2.setStatus(StoplichtStatus.Rood);
this.Refresh();
}
Another option is to use a Timer:
public void button1_Click(object sender, EventArgs e)
{
var timer = new System.Windows.Forms.Timer { Interval = 5000 };
timer.Tick += delegate
{
timer.Dispose();
kruispunt.zPad1.voetstoplicht1.setStatus(StoplichtStatus.Rood);
kruispunt.zPad1.voetstoplicht2.setStatus(StoplichtStatus.Rood);
this.Refresh();
}
timer.Start();
}
Note that I used a Windows Forms timer, rather than System.Timers.Timer or System.Threading.Timer; this is because the event must occur in the UI thread, otherwise the call to Refresh would fail.

How to let System.Windows.Forms.Timer to run the Tick handler immediately when starting?

The Timer following is System.Windows.Forms.Timer
C# Code:
Timer myTimer = new Timer();
myTimer.Interval = 10000;
myTimer.Tick += new EventHandler(doSth);
myTimer.Start();
The timer will doSth every 10 seconds, but how to let it doSth immediately when starting?
I tried to create a custom timer which extends Timer, but I can't override the Start method.
For now, my code is:
myTimer.Start();
doSth(this, null);
I don't think it's good. How to improve it?
It's perfect. Don't change a thing.
I'm not sure of your exact requirements but why not put the code inside your timer callback inside another method?
e.g.
private void tmrOneSec_Tick(object sender, System.EventArgs e)
{
DoTimerStuff();
}
private void DoTimerStuff()
{
//put the code that was in your timer callback here
}
So that way you can just call DoTimerStuff() when your application starts up.
The timer has to have form level scope, and it's not clear that you have that. I whipped up a small example out of curiosity and it is working for me:
private void Form1_Load(object sender, EventArgs e)
{
txtLookup.Text = "test";
DoSomething();
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
DoSomething();
}
private void DoSomething()
{
txtLookup.Text += "ticking";
}
Every 10 seconds it appends another "ticking" to the text in the textbox.
You say,
"The timer will doSth every 10 seconds, but how to let it doSth immediately when starting?"
I say, call doSth immediately before calling the timer's start method

windows form keeping datagridview up to date

I have a datagridview that is connected to a table. The datagridview changes when certain actions are made, but I would like it to refresh (i.e. reload the grid from a datatable that I create) even if the user isn't doing anything so the data the user sees is up to date. What is the best way to do this? Thanks!
Try using a timer like this
private void Form1_Load(object sender, EventArgs e)
{
Timer timer = new Timer();
timer.Interval=1000; // time in milliseconds
timer.Tick+=new EventHandler(timer_Tick);
}
void timer_Tick(object sender, EventArgs e)
{
//Do your update here
}

ShowDialog, PropertyGrid and Timer problem

I have a strange bug, please, let me know if you have any clues about the reason.
I have a Timer (System.Windows.Forms.Timer) on my main form, which fires some updates, which also eventually update the main form UI. Then I have an editor, which is opened from the main form using the ShowDialog() method. On this editor I have a PropertyGrid (System.Windows.Forms.PropertyGrid).
I am unable to reproduce it everytime, but pretty often, when I use dropdowns on that property grid in editor it gets stuck, that is OK/Cancel buttons don't close the form, property grid becomes not usable, Close button in the form header doesn't work.
There are no exceptions in the background, and if I break the process I see that the app is doing some calculations related to the updates I mentioned in the beginning.
What can you recommend? Any ideas are welcome.
What's happening is that the thread timer's Tick method doesn't execute on a different thread, so it's locking everything else until it's done. I made a test winforms app that had a timer and 2 buttons on it whose events did this:
private void timer1_Tick(object sender, EventArgs e)
{
Thread.Sleep(6000);
}
private void button1_Click(object sender, EventArgs e)
{
timer1.Start();
}
private void button2_Click(object sender, EventArgs e)
{
frmShow show = new frmShow();
show.ShowDialog(); // frmShow just has some controls on it to fiddle with
}
and indeed it blocked as you described. The following solved it:
private void timer1_Tick(object sender, EventArgs e)
{
ThreadPool.QueueUserWorkItem(DoStuff);
}
private void DoStuff(object something)
{
Thread.Sleep(6000);
}

Categories