473,387 Members | 1,703 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

error creating timer_elapsed handler.

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.Configuratio nSettings.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
Jun 29 '06 #1
3 3928
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" wrote:
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.Configuratio nSettings.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

Jun 29 '06 #2
Thank you Peter. That makes sense. I'm experimenting. -Greg

"Peter Bromberg [C# MVP]" <pb*******@yahoo.nospammin.com> wrote in message
news:90**********************************@microsof t.com...
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" wrote:
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.Configuratio nSettings.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

Jun 30 '06 #3
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.Configuratio nSettings.AppSettings["interval"]);
timer1.Interval = interval;
timer1.Enabled = true;
}
catch(Exception ex)
{
MessageBox.Show("In OnStart Component" + ex.ToString());
}

"Peter Bromberg [C# MVP]" <pb*******@yahoo.nospammin.com> wrote in message
news:90**********************************@microsof t.com...
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" wrote:
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.Configuratio nSettings.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

Jun 30 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Xeon | last post by:
Hi, Is there anyway to set a custom error handler which is actually a method of a class? i.e. setting the method eh() of class foo as error handler in the snippet below. class foo { function...
6
by: DraguVaso | last post by:
Hi, In my application, on some given actions while debugging in Visual Studio, I suddenly get a "System.ComponentModel.Win32Exception was unhandled" Message="Error creating window handle."...
16
by: TD | last post by:
This is the code under a command button - Dim ctl As Control For Each ctl In Me.Controls If ctl.BackColor <> RGB(255, 255, 255) Then ctl.BackColor = RGB(255, 255, 255) End If Next ctl
12
by: Vittorio Pavesi | last post by:
Hello, is it possible to manually raise the event Elapsed of the timer object ? Vittorio
8
by: jcrouse | last post by:
I am using the following code to trap errors in a sub routine: Try Executable code Catch ex As Exception Dim strInputE As String = Application.StartupPath & "\Error.txt" Dim srE As...
8
by: NewUser | last post by:
Hello, I'm a new user to Visual Basic.net and I would appreciate any help regarding a problem I have. I have searched the posts in this newsgroup and the VB library for threading topics, but I...
6
by: =?Utf-8?B?cHJhZGVlcF9UUA==?= | last post by:
I am trying to create a simple HTTP handler in ASP.net 2.0. I am using VS 2005. I am trying to handle a custom extension file givein in the URL. I have also created the following entry in the...
9
by: Daniel Smedegaard Buus | last post by:
Hey all :) I was wondering about the $error_types (I particularly notice the 's' suffix when reading the manual) parameter for 'set_error_handler()': Can be used to mask the triggering of the...
5
by: mrk98 | last post by:
Hi, I am getting the following error when I try to start the windows service I have written: ReadOutlookService on local computer started and then stopped.Some services stop automatically if...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.