Using DispatcherTimer in a Net 6.0 C# Class Library Project

Paul WPF Development 41 Reputation points
2022-07-25T14:57:13.853+00:00

I am trying to use a DispatcherTimer in system instead of a System.Timers.Timer in a C# class library targeting Net 6.0. I have googled and read MSDN article on how to do this and NONE of the solutions work.

DispatcherTimer is not recognized
System.Windows.Threading is not recognized
WIndows.UI.Xaml is not recognized

the following solution below also does not work

https://learn.microsoft.com/en-us/uwp/api/Windows.UI.Xaml.DispatcherTimer?redirectedfrom=MSDN&view=winrt-22621

Windowsbase.dll does NOT exist in Visual Studio when I try to add the project to my Class library project. but I have added WIndowsBase_Core.STW

What do I need to do to use a DispatcherTimer in a Net 6.0 C# class library? this shouldn't be rocket science.

Developer technologies | .NET | Other
Developer technologies | C#
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 122.6K Reputation points
    2022-07-26T09:13:51.807+00:00

    I think that System.Windows.Threading.Dispatcher is available if you create a "WPF Class Library" project.

    If you want to use it in your "Class Library" project, then try opening the project XML (double-click it in Solution Explorer), replace <TargetFramework>net6.0</TargetFramework> with <TargetFramework>net6.0-windows</TargetFramework><UseWPF>true</UseWPF>, and save it.

    0 comments No comments

3 additional answers

Sort by: Most helpful
  1. AgaveJoe 30,126 Reputation points
    2022-07-25T15:04:51.087+00:00

    The DispatcherTimer Class is specific to WinRT. See if the follow refence documentation helps.

    C#/WinRT


  2. ShuaiHua Du 636 Reputation points
    2022-07-25T15:14:23.63+00:00

    On microsoft reference source, you can reference the DispatcherTime.cs.

    Click DispatcherTime.cs to view the source code.


  3. Paul WPF Development 41 Reputation points
    2022-07-26T13:28:47.32+00:00

    Modifying the project XML file to enable WPF did the trick, thank you!

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.