The calling thread cannot access this object because a different thread owns it

前端 未结 2 371
北恋
北恋 2021-01-18 17:32
namespace PizzaSoftware.UI
{
    /// 
    /// Interaction logic for LoginForm.xaml
    /// 
    public partial class LoginForm : Windo         


        
2条回答
  •  礼貌的吻别
    2021-01-18 18:22

    Didn't realize this was a WPF app. You need to get a hold of the Dispatcher, which you can do in the Constructor:

        private Dispatcher _dispathcer;
    
        public LoginForm()
        {
            InitializeComponent();
            _dispathcer = Dispathcer.CurrentDispather;
        }
    
    
    
        void timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            _dispathcer.Invoke(new Action( ()=> { lblCurrentTime.Content = DateTime.Now.ToShortTimeString();});
        }
    

提交回复
热议问题