473,799 Members | 3,101 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.E lapsedEventHand ler(timer1_Elap sed);

An object reference is required for the nonstatic field, method, or property
'TestTimerInter valFromConfig.C lass1.timer1_El apsed(object,
System.Timers.E lapsedEventArgs )'
using System;
using System.Configur ation;
using System.Windows. Forms;
using System.Timers;
namespace TestTimerInterv alFromConfig
{
class Class1
{
public System.Timers.T imer timer1;
[STAThread]
static void Main(string[] args)
{
System.Timers.T imer timer1 = new System.Timers.T imer();
timer1.Elapsed += new
System.Timers.E lapsedEventHand ler(timer1_Elap sed);
double interval =
Convert.ToDoubl e(System.Config uration.Configu rationSettings. AppSettings["interval"]);
timer1.Interval = interval;
timer1.Enabled = true;
}
private void timer1_Elapsed( object sender,
System.Timers.E lapsedEventArgs e)
{
MessageBox.Show ("Refresh the queue with a new view !");
}
}
}

Thanks, -Greg
Jun 29 '06 #1
3 3942
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.E lapsedEventHand ler(timer1_Elap sed);

An object reference is required for the nonstatic field, method, or property
'TestTimerInter valFromConfig.C lass1.timer1_El apsed(object,
System.Timers.E lapsedEventArgs )'
using System;
using System.Configur ation;
using System.Windows. Forms;
using System.Timers;
namespace TestTimerInterv alFromConfig
{
class Class1
{
public System.Timers.T imer timer1;
[STAThread]
static void Main(string[] args)
{
System.Timers.T imer timer1 = new System.Timers.T imer();
timer1.Elapsed += new
System.Timers.E lapsedEventHand ler(timer1_Elap sed);
double interval =
Convert.ToDoubl e(System.Config uration.Configu rationSettings. AppSettings["interval"]);
timer1.Interval = interval;
timer1.Enabled = true;
}
private void timer1_Elapsed( object sender,
System.Timers.E lapsedEventArgs 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*******@yaho o.nospammin.com > wrote in message
news:90******** *************** ***********@mic rosoft.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.E lapsedEventHand ler(timer1_Elap sed);

An object reference is required for the nonstatic field, method, or
property
'TestTimerInter valFromConfig.C lass1.timer1_El apsed(object,
System.Timers.E lapsedEventArgs )'
using System;
using System.Configur ation;
using System.Windows. Forms;
using System.Timers;
namespace TestTimerInterv alFromConfig
{
class Class1
{
public System.Timers.T imer timer1;
[STAThread]
static void Main(string[] args)
{
System.Timers.T imer timer1 = new System.Timers.T imer();
timer1.Elapsed += new
System.Timers.E lapsedEventHand ler(timer1_Elap sed);
double interval =
Convert.ToDoubl e(System.Config uration.Configu rationSettings. AppSettings["interval"]);
timer1.Interval = interval;
timer1.Enabled = true;
}
private void timer1_Elapsed( object sender,
System.Timers.E lapsedEventArgs 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.ServiceP rocess.ServiceB ase
{
static public System.Timers.T imer timer1;
static double interval;
***code **
protected override void OnStart(string[] args)
{
try
{
System.Timers.T imer timer1 = new System.Timers.T imer();
timer1.Elapsed += new
System.Timers.E lapsedEventHand ler(timer1_Elap sed);
interval =
Convert.ToDoubl e(System.Config uration.Configu rationSettings. AppSettings["interval"]);
timer1.Interval = interval;
timer1.Enabled = true;
}
catch(Exception ex)
{
MessageBox.Show ("In OnStart Component" + ex.ToString());
}

"Peter Bromberg [C# MVP]" <pb*******@yaho o.nospammin.com > wrote in message
news:90******** *************** ***********@mic rosoft.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.E lapsedEventHand ler(timer1_Elap sed);

An object reference is required for the nonstatic field, method, or
property
'TestTimerInter valFromConfig.C lass1.timer1_El apsed(object,
System.Timers.E lapsedEventArgs )'
using System;
using System.Configur ation;
using System.Windows. Forms;
using System.Timers;
namespace TestTimerInterv alFromConfig
{
class Class1
{
public System.Timers.T imer timer1;
[STAThread]
static void Main(string[] args)
{
System.Timers.T imer timer1 = new System.Timers.T imer();
timer1.Elapsed += new
System.Timers.E lapsedEventHand ler(timer1_Elap sed);
double interval =
Convert.ToDoubl e(System.Config uration.Configu rationSettings. AppSettings["interval"]);
timer1.Interval = interval;
timer1.Enabled = true;
}
private void timer1_Elapsed( object sender,
System.Timers.E lapsedEventArgs 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
3558
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 eh() { do something } }
6
3255
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." exception. The problem is that this exception isn't raised somewhere in a method, so it just shows up, and it causes the application to shut down. Is there anyway how to catch this kinds of exceptions? Can I put somewhere a
16
7239
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
2945
by: Vittorio Pavesi | last post by:
Hello, is it possible to manually raise the event Elapsed of the timer object ? Vittorio
8
1793
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 StreamWriter = File.AppendText(strInputE) srE.WriteLine(vbCr) srE.WriteLine(vbCr) srE.WriteLine(DateTime.Now)
8
2449
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 get confused with the code that is displayed. Anyway, I have a System.Timers.Timer object in a form and its interval is 50 ms. I want to create a thread around the Timer_Elapsed event (ie. myThread = new System.Threading.Thread(AddressOf...
6
2841
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 web.config file <httpHandlers> <add verb="*" path="*.imgw" type="Customhandler.Handler,Handler" /> </httpHandlers> following is the code in Handler.ashx file
9
2274
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 error_handler function just like the error_reporting ini setting controls which errors are shown. Without this mask set the error_handler will be called for every error regardless to the setting of the error_reporting setting.
5
3454
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 they have no work to do, for example the Performance logs and alerts service. My Service.cs contains the following: protected override void OnStart(string args) {
0
10490
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10260
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10243
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10030
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9078
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5467
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5590
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4146
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3762
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.