timer ambiguous

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I trying to setup a time. The following code is in a function,

Timer stateTimer = new Timer(ab(), null, 1000, 1000);

I have included

using System.Threading;

at the begining of my code. When I try to compile I receive the error,

'Timer' is an ambiguous reference

What is the problem?
 
You should put the full namespace path in your constructor.

ex:
System.Threading.Timer stateTimer = new System.Threading.Timer(ab(),
null, 1000, 1000);
 
There are three timers:
System.Timers.Timer
System.Windows.Form.Timer
System.Threading.Timer

Decide which one you want to use and write explicit definition, like:
System.Timers.Timer stateTimer = new System.Timers.Timer(ab(), null, 1000,
1000);
 
That fixed my original problem. I' ve changed the code to;

System.Threading.Timer stateTimer = new System.Threading.Timer(ab(), null,
1000, 1000);

Now I'm receiving the error, "Argument '1': cannot convert from 'void' to
'System.Threading.TimerCallback'

My function is simply as follows, which simply increments a counter;

public void ab()
{
t_count +=1;
}

I don't see that thee is anything to convert. What's causing this error?
 
Are you sure you need the threading timer? it is more complicated than the
others.
If you still want to use it:

TimerCallback timerDelegate = new TimerCallback(ab);
Timer timer = new Timer(timerDelegate, null, 1000, 1000);

public void ab(object Status)
{
t_count +=1;
}
 
I start the timer when I click one button. I want to use another button's
click event to stop the timer by calling the dispose function. However I
receive the error,

The type or namespace name 'stateTime' could not be found.

How do I reference the timer in the other button's function?
 
Two things,
1. define the first param of the timer constructor as a TimerCallback
object... (remove the parentheses after the function "ab" reference)

System.Threading.Timer stateTimer = new System.Threading.Timer(new
TimerCallback(ab), null,
1000, 1000);


But then the function that you are calling has to fit the
TimerCallback definition...change it to this and it should work...

[code:1:19016fe374]
public void ab( Object state )
[/code:1:19016fe374]

Hope this helps,
Joe
 
If the two buttons are in the same class, declare the timer as a class
variable and you can reference it anywhere in the class. If not, pass a
reference to the timer to the second form in its constructor.
You can use System.Timers.Timer or System.Windows.Forms.Timer. They have
start and stop methods.
 
It sounds like the stateTimer object is being declared in the scope of
your start button code. You'll need to declare it more global in the
scope of your class so that the stop button code has visibility of
the object.


using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Threading;

namespace EnvVar
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class frmMain : System.Windows.Forms.Form
{
System.Threading.Timer oTimer; //visible to entire class code
int iCount;

private System.Windows.Forms.Button StartButton;
private System.Windows.Forms.Button StopButton;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public frmMain()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent
call
//
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.StartButton = new System.Windows.Forms.Button();
this.StopButton = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// StartButton
//
this.StartButton.Location = new System.Drawing.Point(208,
192);
this.StartButton.Name = "StartButton";
this.StartButton.TabIndex = 0;
this.StartButton.Text = "button1";
this.StartButton.Click += new
System.EventHandler(this.StartButton_Click);
//
// StopButton
//
this.StopButton.Location = new System.Drawing.Point(192,
232);
this.StopButton.Name = "StopButton";
this.StopButton.TabIndex = 1;
this.StopButton.Text = "button2";
this.StopButton.Click += new
System.EventHandler(this.StopButton_Click);
//
// frmMain
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.Add(this.StopButton);
this.Controls.Add(this.StartButton);
this.Name = "frmMain";
this.Text = "Form1";
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new frmMain());
}

private void StartButton_Click(object sender, System.EventArgs
e)
{
oTimer = new System.Threading.Timer( new
TimerCallback(ab), null, 1000, 1000 );
}


private void StopButton_Click(object sender, System.EventArgs
e)
{
oTimer.Dispose();
}

public void ab( Object state )
{
iCount++;
}
}
}
 
Back
Top