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 8708101
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T04:02:28+00:00 2026-06-13T04:02:28+00:00

When I call the PressCommand.RaiseCanExecuteChanged(); in the TimerOnElapsed method, nothing happened. What could be

  • 0

When I call the PressCommand.RaiseCanExecuteChanged(); in the TimerOnElapsed method, nothing happened.

What could be the problem?
(GalaSoft.MvvmLight.WPF4 v4.0.30319 and GalaSoft.MvvmLight.Extras.WPF4 v4.0.30319)

Here is my test code:

using System.Timers;
using System.Windows;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;

namespace CommandTest {


public class MainWindowVM : ViewModelBase {

    public MainWindowVM() {

        PressCommand = new RelayCommand(
                            () => MessageBox.Show("Pressed"),
                            () => _canExecute);

        PressCommand.CanExecuteChanged += (sender, args) => System.Diagnostics.Debug.WriteLine(System.DateTime.Now.ToLongTimeString() + " CanExecuteChanged");

        _timer = new Timer(1000);
        _timer.Elapsed += TimerOnElapsed;
        _timer.Enabled = true;
    }

    public RelayCommand PressCommand { get; private set; }

    #region Private

    private void TimerOnElapsed(object sender, ElapsedEventArgs elapsedEventArgs) {
        _canExecute = !_canExecute;
        PressCommand.RaiseCanExecuteChanged();

        System.Diagnostics.Debug.WriteLine("At {0} enabled: {1}", elapsedEventArgs.SignalTime.ToLongTimeString(), _canExecute);
    }

    private readonly Timer _timer;
    private bool _canExecute;

    #endregion


}
}

Thank you in advance

  • 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-13T04:02:29+00:00Added an answer on June 13, 2026 at 4:02 am

    Explanation:

    The TimerOnElapsed method runs on a Worker Thread but to invoke the PressCommand.RaiseCanExecuteChanged(); must be on the UI thread.

    So this is the solution, the updated TimerOnElapsed method:

        private void TimerOnElapsed(object sender, ElapsedEventArgs elapsedEventArgs) {
            _canExecute = !_canExecute;
            Application.Current.Dispatcher.Invoke(PressCommand.RaiseCanExecuteChanged);
            System.Diagnostics.Debug.WriteLine("At {0} enabled: {1}", elapsedEventArgs.SignalTime.ToLongTimeString(), _canExecute);
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Call to undefined method after upgrading to PHP 5.4.0 mentions a few APC bug-reports
I call a javascript function from a textbox by using OnKeyPress=clickSearchButton() Here is my
i call google.wallet.online.authorize() method to determine if user is pre authorized to Google Wallet
I call my swap method in main, but it doesn't change anything. What am
Call to a possibly undefined method test through a reference with static type Class.
I call an async method which in turn calls needs to start several async
I call window.location.reload(false) in a javascript method to update the page. I have additional
I call a method from Ajax. The method returns string. After the method finishes,
i call a simple endpoint-method from client. MyStatus getStatus(Car car) throws NoSuchCarException; what i
Problem: Call to send() , returns Winsock Error 10038 against socket handle Illustration: acceptedSocket

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.