Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / June 2006

Tip: Looking for answers? Try searching our database.

error creating timer_elapsed handler.

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
hazz - 30 Jun 2006 00:25 GMT
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
Peter Bromberg [C# MVP] - 30 Jun 2006 00:40 GMT
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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.