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
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

Signature
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com
> Why do I get this error for this simple console app. It is complaining about
> the 'timer1_Elapsed' argument in
[quoted text clipped - 33 lines]
>
> Thanks, -Greg
hazz - 30 Jun 2006 17:56 GMT
Thank you Peter. That makes sense. I'm experimenting. -Greg
> hazz,
> Since your Main method is static, so too must be your timer1 object and
[quoted text clipped - 48 lines]
>>
>> Thanks, -Greg
hazz - 30 Jun 2006 18:36 GMT
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());
}
> hazz,
> Since your Main method is static, so too must be your timer1 object and
[quoted text clipped - 48 lines]
>>
>> Thanks, -Greg