2

I am trying to use a dispatch timer, but my c# app can't find the namespace. This is the error:

The type or namespace name 'DispatcherTimer' could not be found (are you missing a using directive or an assembly reference?)

Here is what I am using:

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Globalization;
using System.Text;
using System.Threading;
using System.Timers;
using System.Windows.Forms;

Here is the code:

DispatcherTimer timer1 = new DispatcherTimer();
Michael
  • 57,169
  • 9
  • 80
  • 125
nate
  • 1,418
  • 5
  • 34
  • 73
  • Have you added a reference in the project references for this namespace? According to [this](http://msdn.microsoft.com/en-us/library/system.windows.threading.dispatchertimer.aspx) `DispatcherTimer` lives in the `System.Windows.Threading` namespace. – Brian Apr 09 '13 at 16:26
  • I don't have the option to add that, only choices after system.windows is forms and xps – nate Apr 09 '13 at 16:30

5 Answers5

9

DispatcherTimer is not a namespace - it's a class within the System.Windows.Threading namespace and the WindowsBase assembly. So you need

using System.Windows.Threading;

In general, a search for the missing type name and "MSDN" is enough to find out where to find a type.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • I don't have the option to add that, only choices after system.windows is forms and xps – nate Apr 09 '13 at 16:36
  • 2
    @nate: Then it sounds like you're not writing a WPF or Silverlight app - it sounds like you're writing a Windows Forms app, and `DispatcherTimer` isn't meant for that. Look at `System.Windows.Forms.Timer` instead. – Jon Skeet Apr 09 '13 at 16:39
  • oh, I thought you could use dispatchertimer in any project type. – nate Apr 09 '13 at 16:40
  • okay, well I guess I will us something else. Thanks for the help – nate Apr 09 '13 at 16:43
  • @nate: I've already suggested the "something else" to use - `System.Windows.Forms.Timer`. It's a timer which always fires on the UI thread, which I assume is what you're after. – Jon Skeet Apr 09 '13 at 16:48
2

The Dispatcher class is in WindowsBase.dll. You MUST add that reference to your project to use "using System.Windows.Threading;" <>

1

Add this using:

using System.Windows.Threading;

MSDN

gdoron
  • 147,333
  • 58
  • 291
  • 367
  • I don't have the option to add that, only choices after system.windows is forms and xps – nate Apr 09 '13 at 16:31
0

Isn't in the System.Windows.Threading namespace and not the System.Threading namespace?

http://msdn.microsoft.com/en-us/library/system.windows.threading.dispatchertimer.aspx

sous2817
  • 3,915
  • 2
  • 33
  • 34
  • I don't have the option to add that, only choices after system.windows is forms and xps – nate Apr 09 '13 at 16:35
0

You must Add WindowsBase in your references so you can use the System.Windows.Threading.