2

I'm just using some tests where in certain cases I need a thread (again just for test purposes) and some of them I need a Timer.

am I doing something or can't Timer and Thread exist in the same "class" in my case a Form class? (Form1.cs)

I'm importing these libraries

using System;
//using System.Threading;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Media;
using System.Collections;

as you can see I have commented System.Threading as it doesn't let me use it with Threading.Tasks (and this gives me Threading does not exist in this context). When I try to uncomment that Threading it doesn't let me saying there's an ambigious reference

Am I doing something wrong or is this a C# .NET Standard that you cannot have those 2 together?

Also if this is the case... why can't Timer and Thread be in the same class

Thank you

DodoSombrero
  • 767
  • 3
  • 15
  • 29
  • 2
    Sounds like the same class exists in multiple namespaces, hence the ambiguous reference – jglouie Dec 10 '13 at 11:51
  • [This](http://stackoverflow.com/q/5717278/969613) may help – JMK Dec 10 '13 at 11:51
  • But when I removed System.Threading... Thread.Sleep(200) didn't work and when I removed System.Threading.Tasks and left the System.Threading, the timer didn't work – DodoSombrero Dec 10 '13 at 11:51
  • You don't have to add a namespace to the top of the class, as an alternative you can use the [fully qualified name](http://stackoverflow.com/questions/15392766/in-c-is-it-more-performant-to-use-fully-qualified-names-vs-the-using-directi) in your code. – JMK Dec 10 '13 at 11:53
  • Does `System.Threading.Thread.Sleep(200)` work? – jglouie Dec 10 '13 at 11:53
  • `System.Windows.Forms` and `System.Threading` both contain `Timer` classes. you will need to alias one of the namespaces as shown in the answers below. – Gusdor Dec 10 '13 at 11:57

4 Answers4

6

Why can't I use Timer and Thread in same class?

The problem is:

There is a Timer class in both namespaces:

System.Timers.Timer;
System.Threading.Timer;

So .NET is not able to determine which timer you want to use if you just declare a timer like:

Timer timer; // Compile Error

You need to be more specific so that the compiler knows which timer you want to use:

System.Timers.Timer timer = new System.Timers.Timer();
System.Threading.Timer otherTimer = new System.Threading.Timer(MyCallBackMethod);

If you want to use the timer class from System.Timers together with components from System.Threading you can decide to use an namespace alias for the System.Timers namespace to make code more readable:

using t = System.Timers;

public void MyMethod()
{
   t.Timer = new t.Timer(); 
   // this code is equals to 
   // System.Timers.Timer timer = new System.Timers.Timer();
}
hwcverwe
  • 5,287
  • 7
  • 35
  • 63
  • Thansk Very much, it worked. C# was telling me that it was confused not knowing which to use.. to be honest I didn't know Timer was in 2 subclasses.. Thanks a lot – DodoSombrero Dec 10 '13 at 12:16
5

You can alias your namespace to differentiate between them

using System;
// other usings
using tAlias = System.Threading;

// use alias
public void Sample()
{
   var thread = new tAlias.Thread( () => { Trace.WriteLine(" demo that thread works"); });
   tAlias.Thread.Sleep(10);
}

// use full namespace 
public void Sample()
{
   var timer = new System.Threading.Timer( () => { Trace.WriteLine(" timer called"); });
   timer.Change(0,5000);
   System.Threading.Thread.Sleep(10);
}

If two types with the same name exsits there is no way the compiler (or you) can determine which type you want to use. Timer from System.Windows.Form or Timer from System.Threading. That is where namespaces come into play. Every type lives in his own namespace and you can pinpoint them with their full namespce name. In code that would be:

System.Threading.Timer 

or

System.Windows.Forms.Timer

If ambiguity exists the namespace comes to the rescue. To make life easier you can introduce an alias to reduce the typing you have to do and to have a little bit less code to stare at.

Because in a Windows form class it is most likely you are going to have a lot of types from the System.Windows.Forms namespace you leave that using as is and add an alias for the 'extra' imported namespace System.Threading.

rene
  • 41,474
  • 78
  • 114
  • 152
2

You're having a namespace conflict, here are 2 solutions:

using System;

...

var tmrTimer = new Timers.Timer();
var thrTimer = new Threading.Timer();

OR

using System;
using timerTimer = System.Timers.Timer;
using threadTimer = System.Threading.Timer;

...

var tmrTimer = new timerTimer();
var thrTimer = new threadTimer();
  • I have the Timer which needs to be used from OTHER METHODS... with VAR I don't think I can make it public no? as In the variable scope is only in that method even if it is public or I'm wrong? – DodoSombrero Dec 10 '13 at 12:18
  • @DodoSerebro Just replace `var` with the classname and you're good to go. –  Dec 10 '13 at 12:19
  • Thanks a lot :) All of you for your help – DodoSombrero Dec 10 '13 at 13:36
  • I'm using System.Threading.Timer t1 = new System.Threading.Timer(); but it's telling me it doesn't accept 0 arguments (the RHS) – DodoSombrero Dec 10 '13 at 13:53
  • Yeah when you create your instance you should pass some arguments. E.g: `System.Threading.Timer t1 = new System.Threading.Timer(ARGUMENTS...);` –  Dec 10 '13 at 14:16
  • But when I used timer t1 = new timer() (when I didn't set the system.windows... etc) it accepted it – DodoSombrero Dec 10 '13 at 14:19
  • @DodoSerebro Because it automatically uses `System.Timers` and not the `Threading` timer. –  Dec 10 '13 at 14:22
0

It's just a name collision because Timeris defined many times in many namespaces.

 using MyTimer = System.Threading.Timer;

will probably fix it

TheQult
  • 364
  • 1
  • 3
  • 14