The calling thread cannot access this object - Timer [duplicate]

微笑、不失礼 提交于 2019-12-13 10:59:45

问题


I am setting up a Timer within a method with an interval of 1000 so that every second it will type another corresponding character into a Textbox (pretty much automating typing). When I check for _currentTextLength == _text.Length I get the threading error "The calling thread cannot access this object because a different thread owns it."

 public void WriteText(string Text)
    {
        timer = new Timer();

        try
        {
            _text = Text;
            timer.Elapsed += new ElapsedEventHandler(timer_Elapsed_WriteText);
            timer.Interval = 1000;
            timer.Enabled = true;
            timer.Start();
        }
        catch
        {
            MessageBox.Show("WriteText timer could not be started.");
        }
    }
    // Write Text Timer Event
    void timer_Elapsed_WriteText(object sender, ElapsedEventArgs e)
    {
        TextBoxAutomationPeer peer = new TextBoxAutomationPeer(_textBox);
        IValueProvider valueProvider = peer.GetPattern(PatternInterface.Value) as IValueProvider;

        valueProvider.SetValue(_text.Substring(0, _currentTextLength));
        if (_currentTextLength == _text.Length) // Error here
        {
            timer.Stop();
            timer = null;
            return;
        }

        _currentTextLength++;
    }

The variable _text is a private class variable and so is _currentTextLength. _textBox is self explanatory.

Any way to solve this?


回答1:


Use a DispatcherTimer instead of a Timer.

A timer that is integrated into the Dispatcher queue which is processed at a specified interval of time and at a specified priority.

Should solve your problem.




回答2:


this simply means that you are trying to access some UI element from a thread other then it was created on. To overcome this you need to access it like this

this.Dispatcher.Invoke((Action)(() =>
    {
        //access it here
    }));

Note: If you want to check whether you can access it normally or not you can use this

Dispatcher.CheckAccess


来源:https://stackoverflow.com/questions/17954756/the-calling-thread-cannot-access-this-object-timer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!