Clock program employing ThreadPoolTimer C# uwp

雨燕双飞 提交于 2019-12-25 06:59:36

问题


this is modelled on a answer to a previous question How to display a clock with the current time in a Windows Core IoT app?, I get an exception: "The application called an interface that was marshalled for a different thread." If I change the property with an click event, the binding works.

public class RunClock: INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged = delegate { };
    ThreadPoolTimer _clockTimer = null;
    private string clock="";
    public string Clock
    {
        set
        {
            clock= value;
            this.OnPropertyChanged();
        }
        get { return clock; } }
    public RunClock()
    {

        _clockTimer = ThreadPoolTimer.CreatePeriodicTimer(_clockTimer_Tick, TimeSpan.FromMilliseconds(1000));            
    private void _clockTimer_Tick(ThreadPoolTimer timer)
    {
        DateTime datetime = DateTime.Now;
        Str1 = datetime.ToString();
    }
    public void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}

and the Xaml:

<TextBlock Margin="15" Name="timec" Text="{x:Bind Path=RunClock.Clock, Mode=OneWay}"></TextBlock>

Thank you in advance!

Update, working code:

 public class test : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged = delegate { };
    ThreadPoolTimer _clockTimer = null;
    private string str1 = "";
    public string Str1
    {
        set
        {
            str1 = value;
            this.OnPropertyChanged();
        }
        get { return str1; }
    }
    public test()
    {

        _clockTimer = ThreadPoolTimer.CreatePeriodicTimer(_clockTimer_Tick, TimeSpan.FromMilliseconds(1000));
                }
    async private void _clockTimer_Tick(ThreadPoolTimer timer)
    {
       await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,() =>
        {
            DateTime dt = DateTime.Now;
            Str1 = dt.ToString();
        });
    }
    public void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}

Xaml (test1 is an instance of he viewmodel):

                <TextBlock Margin="15" Name="timec" Text="{x:Bind test1.Str1, Mode=OneWay}"></TextBlock>

回答1:


Exception is because your trying to modify UI in different thread

Use this

 private void _clockTimer_Tick(ThreadPoolTimer timer)
    {
       var dispatcher = Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher;
       await dispatcher.RunAsync(
        CoreDispatcherPriority.Normal, () =>
        {
        // Your UI update code goes here!
        DateTime datetime = DateTime.Now;
        Str1 = datetime.ToString();
        });
    }


来源:https://stackoverflow.com/questions/36994647/clock-program-employing-threadpooltimer-c-sharp-uwp

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