Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8780151
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T19:58:15+00:00 2026-06-13T19:58:15+00:00

I am writing a WPF application. I want to trigger an event once the

  • 0

I am writing a WPF application.
I want to trigger an event once the mouse stops moving.

This is how I tried to do it. I created a timer which counts down to 5 seconds. This timer is “reset” every time the mouse moves.
This idea is that the moment the mouse stops moving, the timer stops being reset, and counts down from 5 to zero, and then calls the tick event handler, which displays a message box.

Well, it doesn’t work as expected, and it floods me with alert messages. What am I doing wrong?

DispatcherTimer timer;

private void Window_MouseMove(object sender, MouseEventArgs e)
{
    timer = new DispatcherTimer();
    timer.Interval = new TimeSpan(0, 0, 5);
    timer.Tick += new EventHandler(timer_Tick);
    timer.Start();
}

void timer_Tick(object sender, EventArgs e)
{
    MessageBox.Show("Mouse stopped moving");
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T19:58:17+00:00Added an answer on June 13, 2026 at 7:58 pm

    You need to unhook the event before hooking it again like this –

    private void poc_MouseMove(object sender, MouseEventArgs e)
    {
       if (timer != null)
       {
          timer.Tick-= timer_Tick;
       }
       timer = new DispatcherTimer();
       timer.Interval = new TimeSpan(0, 0, 5);
       timer.Tick += new EventHandler(timer_Tick);
       timer.Start();
    }
    

    Explanation

    What you doing is whenever a mouse moves, you create a new instance of DispatcherTimer and hook Tick event to it without unhooking the event for previous instance. Hence, you see flooded messages once timer stops for all the instances.

    Also, you should unhook it otherwise previous instance won’t be garbage collected since they are still strongly referenced.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a WPF application in which i want to display the currency symbol
I'm writing a WPF application with C# as the code behind and I want
I'm writing a WPF application that uses a component, and this component returns a
I am writing a .NET 3.5 WPF application in C#. This application needs to
Im writing WPF application and want to add ability to call jump list and
I am writing a WPF application, and one feature I want to implement is
I'm writing a WPF calculator app. In this application I have TextBox that get
When writing a WPF application using MVVM, I want to use a WCF service,
I am currently writing a WPF application which does command-line argument handling in App.xaml.cs
I'm writing a WPF application which will monitor a feed. When there are new

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.