By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,190 Members | 777 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,190 IT Pros & Developers. It's quick & easy.

timer problem

P: n/a
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() ;

}

}
Nov 16 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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" <hk***@hotmail.com> 写入消息新闻
:#G**************@tk2msftngp13.phx.gbl...
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() ;

}

}

Nov 16 '05 #2

P: n/a
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" <li***@channel.com> 秎ン news:ua**************@TK2MSFTNGP12.phx.gbl
い级糶...
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" <hk***@hotmail.com> 写入消息新闻
:#G**************@tk2msftngp13.phx.gbl...
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() ;

}

}


Nov 16 '05 #3

P: n/a
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" <hk***@hotmail.com> 写入消息新闻
:#A**************@TK2MSFTNGP14.phx.gbl...
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" <li***@channel.com> 秎ン news:ua**************@TK2MSFTNGP12.phx.gbl い级糶...
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" <hk***@hotmail.com> 写入消息新闻
:#G**************@tk2msftngp13.phx.gbl...
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() ;

}

}



Nov 16 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.