none
Timer problem in C# Console Application

    Question

  • Dear all, I am trying to do a C# console application that will call the timer class.

    But I currently am having problem at this portion

    Here are my code in the C# console application:--

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data;
    using System.Timers;
    using System.Net.Mail;
    using System.Diagnostics;
    
    
    
    namespace appNonRespondingProcess
    {
        class Program
        {
    
    
            static void Main(string[] args)
            {
                Timer timer;
                string cmd = "";
                System.Console.WriteLine("ENTER ABC:");
                cmd = args[0];//Console.ReadLine();
                timer = new Timer();
                timer.Interval = 10000; //set interval of checking here
                timer.Tick += timer_Tick;
    
                if (cmd.Equals("start"))
                {
                    start_timer();
                }
    
               
                Console.Read();
            }
            private void start_timer()
            {
                timer.Start();
                Console.Write("You entered: " + cmd);
            }
        }
    }
    
    The area which I encountered error are bold and underlined as above:-

    Error message:
    Error 1 'System.Timers.Timer' does not contain a definition for 'Tick' and no extension method 'Tick' accepting a first argument of type 'System.Timers.Timer' could be found (are you missing a using directive or an assembly reference?) C:\Documents and Settings\attcaz1\My Documents\Visual Studio 2008\Projects\appNonRespondingProcess\appNonRespondingProcess\Program.cs 26 19 appNonRespondingProcess

    Error 2 The name 'timer_Tick' does not exist in the current context C:\Documents and Settings\attcaz1\My Documents\Visual Studio 2008\Projects\appNonRespondingProcess\appNonRespondingProcess\Program.cs 26 27 appNonRespondingProcess

    Error 3 An object reference is required for the non-static field, method, or property 'appNonRespondingProcess.Program.start_timer()' C:\Documents and Settings\attcaz1\My Documents\Visual Studio 2008\Projects\appNonRespondingProcess\appNonRespondingProcess\Program.cs 30 17 appNonRespondingProcess

    Error 4 The name 'timer' does not exist in the current context C:\Documents and Settings\attcaz1\My Documents\Visual Studio 2008\Projects\appNonRespondingProcess\appNonRespondingProcess\Program.cs 38 13 appNonRespondingProcess

    Error 5 The name 'cmd' does not exist in the current context C:\Documents and Settings\attcaz1\My Documents\Visual Studio 2008\Projects\appNonRespondingProcess\appNonRespondingProcess\Program.cs 39 45 appNonRespondingProcess


    Someone please help me please, thanks :)
    Friday, July 17, 2009 3:06 AM

Answers

  • hello magicball,
    I removed those erros. refer following Code.

    Take good books for c# and Windows programing, need to learn basic concepts.
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data;
    using System.Timers;
    using System.Net.Mail;
    using System.Diagnostics;
    
    
    
    
    namespace appNonRespondingProcess
    {
        class Program
        {
           static Timer timer;
           static string cmd;
    
            static void Main(string[] args)
            {          
                System.Console.WriteLine("ENTER ABC:");
                cmd = args[0];//Console.ReadLine();
                timer = new Timer();
                timer.Interval = 10000; //set interval of checking here
                timer.Elapsed +=new ElapsedEventHandler(timer_Elapsed);
    
                if (cmd.Equals("start"))
                {
                    start_timer();
                }
    
               
                Console.Read();
            }
    
            static void timer_Elapsed(object sender, ElapsedEventArgs e)
            {
                Console.WriteLine("Timer Elapsed");
              
            }
            private static void start_timer()
            {
                timer.Start();
                Console.Write("You entered: " + cmd);
            }
        }
    }
    • Proposed as answer by Matthew Watson Friday, July 17, 2009 9:09 AM
    • Marked as answer by Bin-ze Zhao Wednesday, July 22, 2009 3:38 AM
    Friday, July 17, 2009 7:26 AM