error creating timer_elapsed handler.

H

hazz

Why do I get this error for this simple console app. It is complaining about
the 'timer1_Elapsed' argument in
System.Timers.ElapsedEventHandler(timer1_Elapsed);

An object reference is required for the nonstatic field, method, or property
'TestTimerIntervalFromConfig.Class1.timer1_Elapsed(object,
System.Timers.ElapsedEventArgs)'


using System;
using System.Configuration;
using System.Windows.Forms;
using System.Timers;
namespace TestTimerIntervalFromConfig
{
class Class1
{
public System.Timers.Timer timer1;
[STAThread]
static void Main(string[] args)
{
System.Timers.Timer timer1 = new System.Timers.Timer();
timer1.Elapsed += new
System.Timers.ElapsedEventHandler(timer1_Elapsed);
double interval =
Convert.ToDouble(System.Configuration.ConfigurationSettings.AppSettings["interval"]);
timer1.Interval = interval;
timer1.Enabled = true;
}
private void timer1_Elapsed(object sender,
System.Timers.ElapsedEventArgs e)
{
MessageBox.Show("Refresh the queue with a new view !");
}
}
}

Thanks, -Greg
 
G

Guest

hazz,
Since your Main method is static, so too must be your timer1 object and the
timer1_Elapsed handler. In a static method there is no class instance
object, so you need to make your other class objects and methods static too.

If you had created a separate class and were using an instance of the class,
that would be different.

Peter
 
H

hazz

Thank you Peter. That makes sense. I'm experimenting. -Greg

Peter Bromberg said:
hazz,
Since your Main method is static, so too must be your timer1 object and
the
timer1_Elapsed handler. In a static method there is no class instance
object, so you need to make your other class objects and methods static
too.

If you had created a separate class and were using an instance of the
class,
that would be different.

Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com




hazz said:
Why do I get this error for this simple console app. It is complaining
about
the 'timer1_Elapsed' argument in
System.Timers.ElapsedEventHandler(timer1_Elapsed);

An object reference is required for the nonstatic field, method, or
property
'TestTimerIntervalFromConfig.Class1.timer1_Elapsed(object,
System.Timers.ElapsedEventArgs)'


using System;
using System.Configuration;
using System.Windows.Forms;
using System.Timers;
namespace TestTimerIntervalFromConfig
{
class Class1
{
public System.Timers.Timer timer1;
[STAThread]
static void Main(string[] args)
{
System.Timers.Timer timer1 = new System.Timers.Timer();
timer1.Elapsed += new
System.Timers.ElapsedEventHandler(timer1_Elapsed);
double interval =
Convert.ToDouble(System.Configuration.ConfigurationSettings.AppSettings["interval"]);
timer1.Interval = interval;
timer1.Enabled = true;
}
private void timer1_Elapsed(object sender,
System.Timers.ElapsedEventArgs e)
{
MessageBox.Show("Refresh the queue with a new view !");
}
}
}

Thanks, -Greg
 
H

hazz

Thanks Peter. I got the configurable timer interval service to work !
I moved all the code to the onstart and made the timer and interval
static at the class level
Relevant code is below.
Thanks again. -Greg

public class Service1 : System.ServiceProcess.ServiceBase
{
static public System.Timers.Timer timer1;
static double interval;
***code **
protected override void OnStart(string[] args)
{
try
{
System.Timers.Timer timer1 = new System.Timers.Timer();
timer1.Elapsed += new
System.Timers.ElapsedEventHandler(timer1_Elapsed);
interval =
Convert.ToDouble(System.Configuration.ConfigurationSettings.AppSettings["interval"]);
timer1.Interval = interval;
timer1.Enabled = true;
}
catch(Exception ex)
{
MessageBox.Show("In OnStart Component" + ex.ToString());
}

Peter Bromberg said:
hazz,
Since your Main method is static, so too must be your timer1 object and
the
timer1_Elapsed handler. In a static method there is no class instance
object, so you need to make your other class objects and methods static
too.

If you had created a separate class and were using an instance of the
class,
that would be different.

Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com




hazz said:
Why do I get this error for this simple console app. It is complaining
about
the 'timer1_Elapsed' argument in
System.Timers.ElapsedEventHandler(timer1_Elapsed);

An object reference is required for the nonstatic field, method, or
property
'TestTimerIntervalFromConfig.Class1.timer1_Elapsed(object,
System.Timers.ElapsedEventArgs)'


using System;
using System.Configuration;
using System.Windows.Forms;
using System.Timers;
namespace TestTimerIntervalFromConfig
{
class Class1
{
public System.Timers.Timer timer1;
[STAThread]
static void Main(string[] args)
{
System.Timers.Timer timer1 = new System.Timers.Timer();
timer1.Elapsed += new
System.Timers.ElapsedEventHandler(timer1_Elapsed);
double interval =
Convert.ToDouble(System.Configuration.ConfigurationSettings.AppSettings["interval"]);
timer1.Interval = interval;
timer1.Enabled = true;
}
private void timer1_Elapsed(object sender,
System.Timers.ElapsedEventArgs e)
{
MessageBox.Show("Refresh the queue with a new view !");
}
}
}

Thanks, -Greg
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top