timer problem

J

jam

Dear all,

I am wrtiing a console application and doing some test on timer, the below
is my sample code I got from some site, but I cannot make it work...
Error is
'System.Timers.Timer' does not contain a definition for 'Tick'
So what should i do???

the final thing I wanna get is, I have a console will call a exe running in
background, and then I wanna check it is is still running ( Use get process
by name here to check if it is still running), then if running, check it
after 1 min.
Thx for helping.

Codes:
int numbertext =0;
void TickHandler( object sender, EventArgs e )
{
Console.WriteLine(numbertext);
numbertext ++;
}

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{

// usage block
{

// create timer
Timer kicker = new Timer() ;
kicker.Interval = 1000 ;
kicker.AutoReset = false ;

// add handler
kicker.Tick += new EventHandler( TickHandler ) ;

// start timer
kicker.Start() ;

// change interval
kicker.Interval = 2000 ;

// stop timer
kicker.Stop() ;

// you can start and stop timer againg
kicker.Start() ;
kicker.Stop() ;

}

}
 
A

Alan

Hello jam,
There are three kinds of Timer in .Net totally for different
environment,which is under different namespace, namely System.Threading,
System.Timers, System.Windows.Forms.
According to your aspect, you can only use the Elapsed event, because the
Tick event only exists in the Timer of System.Windows.Forms.
 
J

jam

Thx Alan,

I just found out that too, I modify the code as below, but still have error
An object reference is required for the nonstatic field, method, or property
'SiteStagerDotNet.SiteStager.TickHandler(object,
System.Timers.ElapsedEventArgs)'

Any clue?
code:
int numbertext =0;
private void TickHandler(object sender, ElapsedEventArgs e)
{
Console.WriteLine(numbertext);
numbertext ++;
}

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{

// usage block
{

// create timer
Timer kicker = new Timer() ;
kicker.Interval = 1000 ;
kicker.AutoReset = false ;

// add handler
kicker.Elapsed += new ElapsedEventHandler(TickHandler) ;

// start timer
kicker.Start() ;

// change interval
kicker.Interval = 2000 ;

// stop timer
kicker.Stop() ;

// you can start and stop timer againg
kicker.Start() ;
kicker.Stop() ;

}
Alan said:
Hello jam,
There are three kinds of Timer in .Net totally for different
environment,which is under different namespace, namely System.Threading,
System.Timers, System.Windows.Forms.
According to your aspect, you can only use the Elapsed event, because the
Tick event only exists in the Timer of System.Windows.Forms.

jam said:
Dear all,

I am wrtiing a console application and doing some test on timer, the below
is my sample code I got from some site, but I cannot make it work...
Error is
'System.Timers.Timer' does not contain a definition for 'Tick'
So what should i do???

the final thing I wanna get is, I have a console will call a exe running in
background, and then I wanna check it is is still running ( Use get process
by name here to check if it is still running), then if running, check it
after 1 min.
Thx for helping.

Codes:
int numbertext =0;
void TickHandler( object sender, EventArgs e )
{
Console.WriteLine(numbertext);
numbertext ++;
}

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{

// usage block
{

// create timer
Timer kicker = new Timer() ;
kicker.Interval = 1000 ;
kicker.AutoReset = false ;

// add handler
kicker.Tick += new EventHandler( TickHandler ) ;

// start timer
kicker.Start() ;

// change interval
kicker.Interval = 2000 ;

// stop timer
kicker.Stop() ;

// you can start and stop timer againg
kicker.Start() ;
kicker.Stop() ;

}

}
 
A

Alan

Hello jam,
Yes.Because the Main(...) is a static method.You have two options:
One is to construct one object which has the TickHandler method fit for
ElapsedEventHandler.
The other is to write one static TickHandler method.

jam said:
Thx Alan,

I just found out that too, I modify the code as below, but still have error
An object reference is required for the nonstatic field, method, or property
'SiteStagerDotNet.SiteStager.TickHandler(object,
System.Timers.ElapsedEventArgs)'

Any clue?
code:
int numbertext =0;
private void TickHandler(object sender, ElapsedEventArgs e)
{
Console.WriteLine(numbertext);
numbertext ++;
}

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{

// usage block
{

// create timer
Timer kicker = new Timer() ;
kicker.Interval = 1000 ;
kicker.AutoReset = false ;

// add handler
kicker.Elapsed += new ElapsedEventHandler(TickHandler) ;

// start timer
kicker.Start() ;

// change interval
kicker.Interval = 2000 ;

// stop timer
kicker.Stop() ;

// you can start and stop timer againg
kicker.Start() ;
kicker.Stop() ;

}
"Alan" <[email protected]> ¦b¶l¥ó news:[email protected]
¤¤¼¶¼g...
Hello jam,
There are three kinds of Timer in .Net totally for different
environment,which is under different namespace, namely System.Threading,
System.Timers, System.Windows.Forms.
According to your aspect, you can only use the Elapsed event, because the
Tick event only exists in the Timer of System.Windows.Forms.

jam said:
Dear all,

I am wrtiing a console application and doing some test on timer, the below
is my sample code I got from some site, but I cannot make it work...
Error is
'System.Timers.Timer' does not contain a definition for 'Tick'
So what should i do???

the final thing I wanna get is, I have a console will call a exe
running
in
background, and then I wanna check it is is still running ( Use get process
by name here to check if it is still running), then if running, check it
after 1 min.
Thx for helping.

Codes:
int numbertext =0;
void TickHandler( object sender, EventArgs e )
{
Console.WriteLine(numbertext);
numbertext ++;
}

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{

// usage block
{

// create timer
Timer kicker = new Timer() ;
kicker.Interval = 1000 ;
kicker.AutoReset = false ;

// add handler
kicker.Tick += new EventHandler( TickHandler ) ;

// start timer
kicker.Start() ;

// change interval
kicker.Interval = 2000 ;

// stop timer
kicker.Stop() ;

// you can start and stop timer againg
kicker.Start() ;
kicker.Stop() ;

}

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top