Windows Develop Bookmark and Share   
 index > Windows Forms General > problems with Timer
 

problems with Timer

I have written a simple program to access a web page and have it refresh every minute. To get to refresh automatically,I have inserted a timer. I have also included a thread to write a message in a text box whenever the page is refreshed. I got much of the code verbatim from the MSDN docs. However, I can't get it to work.

Below is the code. If anybody has any ideas, I'm all ears. If somebody can give me some tips on debugging a Timer that would be greatly appreciated as well.

private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
//DialogResult result;
string theUrl = "http://10.170.8.133:8080/OvisDashboard/jsp/StatusWorkspaceView.jsp";
if (webBrowser1.Url.ToString() != theUrl)
{
this.webBrowser1.Navigate(new Uri(theUrl));
}

initTimer();

}

private void startTextThread()
{
this.SetText("Web page refreshed.");

}

private void SetText(string text)
{
if (this.textBox1.InvokeRequired)
{
SetTextCallback d = new SetTextCallback(SetText);
this.Invoke(d, new object[] { text });
}
else
{
this.textBox1.Text = text;
}

}
private void initTimer()
{
tmr=new System.Timers.Timer(60000);
tmr.Elapsed+=new ElapsedEventHandler(tmr_Elapsed);
tmr.AutoReset=true;
tmr.Enabled = true;

}

private void tmr_Elapsed(object source, ElapsedEventArgs e)
{
string refreshMsg = "Web page refreshed.";

textBox1.Text = refreshMsg;

this.myThread = new Thread(new ThreadStart(this.startTextThread));

elipford  Wednesday, May 23, 2007 1:11 PM
You sure are making it hard on yourself with all these threads. Just use a regular Timer:

public partial class Form1 : Form {
private const string MYURL = "http://www.google.com";
public Form1() {
InitializeComponent();
webBrowser1.DocumentCompleted += Completed;
timer1.Interval = 60000;
timer1.Enabled = true;
timer1.Tick += RefreshPage;
RefreshPage(this, EventArgs.Empty);
}
private void RefreshPage(object sender, EventArgs e) {
textBox1.Text = "Refreshing...";
webBrowser1.Navigate(MYURL);
}
private void Completed(object sender, WebBrowserDocumentCompletedEventArgs e) {
textBox1.Text = "Web page refreshed";
}
}

nobugz  Wednesday, May 23, 2007 3:27 PM
You sure are making it hard on yourself with all these threads. Just use a regular Timer:

public partial class Form1 : Form {
private const string MYURL = "http://www.google.com";
public Form1() {
InitializeComponent();
webBrowser1.DocumentCompleted += Completed;
timer1.Interval = 60000;
timer1.Enabled = true;
timer1.Tick += RefreshPage;
RefreshPage(this, EventArgs.Empty);
}
private void RefreshPage(object sender, EventArgs e) {
textBox1.Text = "Refreshing...";
webBrowser1.Navigate(MYURL);
}
private void Completed(object sender, WebBrowserDocumentCompletedEventArgs e) {
textBox1.Text = "Web page refreshed";
}
}

nobugz  Wednesday, May 23, 2007 3:27 PM

You can use google to search for other answers

Custom Search

More Threads

• Requesting a computation to wait
• How to import from .xls file to dataset?
• Tab Controlling in VS 2008
• Skin
• Windows Forms App Without Forms
• Assembly Icon problem
• PrintPreviewDialog / PrintPreviewControl Problem
• Regarding tab index for toolbar
• Can anyone tell me how to change the location of a button from 223 , 831 to 101 , 831 when another form loads ?
• Possible Bug of the TableLayoutPanel?