Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将ThreadPoolTimer与背景音频UWP一起使用_C#_Win Universal App_Uwp_Dispatchertimer - Fatal编程技术网

C# 将ThreadPoolTimer与背景音频UWP一起使用

C# 将ThreadPoolTimer与背景音频UWP一起使用,c#,win-universal-app,uwp,dispatchertimer,C#,Win Universal App,Uwp,Dispatchertimer,我正在使用BackgroundAudioTask制作一个UWP应用程序。我的应用程序运行得很好。现在,我想在文本块中添加播放音频的当前位置 在执行音频任务之前,我正在使用此方法: private TimeSpan TotalTime; private DispatcherTimer timerRadioTime; private void radioPlayer_MediaOpened(object sender, RoutedEventArgs e)

我正在使用BackgroundAudioTask制作一个UWP应用程序。我的应用程序运行得很好。现在,我想在文本块中添加播放音频的当前位置

在执行音频任务之前,我正在使用此方法:

private TimeSpan TotalTime;
        private DispatcherTimer timerRadioTime;

        private void radioPlayer_MediaOpened(object sender, RoutedEventArgs e)
        {
            TotalTime = radioPlayer.NaturalDuration.TimeSpan;

            // Create a timer that will update the counters and the time slider
            timerRadioTime = new DispatcherTimer();
            timerRadioTime.Interval = TimeSpan.FromSeconds(1);
            timerRadioTime.Tick += TimerRadioTime_Tick;
            timerRadioTime.Start();
        }

        private void TimerRadioTime_Tick(object sender, object e)
        {
            //Check if the audio finished calculate it's total time
            if (radioPlayer.NaturalDuration.TimeSpan.TotalSeconds > 0)
            {
                if (TotalTime.TotalSeconds > 0)
                {
                    // Updating timer
                    TimeSpan currentPos = radioPlayer.Position;
                    var currentTime = string.Format("{0:00}:{1:00}", (currentPos.Hours * 60) + currentPos.Minutes, currentPos.Seconds);
                    radioTimerBlock.Text = currentTime;
                }
            }
        }
当我执行后台任务时,它给了我一个异常。在研究之后,我看到了使用ThreadPoolTimer而不是Dispatcher的建议

我尝试编写此代码(遵循此解决方案:)

这显然是行不通的。应用程序进入该方法而不更新我的UI。我真的不明白计时器应该是什么。有没有关于如何运行的想法?

解决方案:

ThreadPoolTimer timer;

            // for displaying time only
            private void CurrentPlayer_MediaOpened(MediaPlayer sender, object args)
            {
                timer = ThreadPoolTimer.CreatePeriodicTimer(_clockTimer_Tick, TimeSpan.FromSeconds(1));
            }


            private async 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!
                     if (CurrentPlayer.NaturalDuration.TotalSeconds < 0)
                     {
                         TimeSpan currentPos = CurrentPlayer.Position;
                         var currentTime = string.Format("{0:00}:{1:00}", (currentPos.Hours * 60) + currentPos.Minutes, currentPos.Seconds);
                         CurrentPosition.Text = currentTime;
                     }

                 });
            }
ThreadPoolTimer;
//仅用于显示时间
私有无效CurrentPlayer_MediaOpen(MediaPlayer发送方,对象参数)
{
timer=ThreadPoolTimer.CreatePeriodicTimer(_clockTimer_Tick,TimeSpan.FromSeconds(1));
}
专用异步无效\u时钟计时器\u滴答(线程池计时器)
{
var dispatcher=Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.dispatcher;
等待dispatcher.RunAsync(
CoreDispatcherPriority.Normal,()=>
{
//你的UI更新代码在这里!
如果(CurrentPlayer.NaturalDuration.TotalSeconds<0)
{
TimeSpan currentPos=当前播放器位置;
var currentTime=string.Format(“{0:00}:{1:00}”,(currentPos.Hours*60)+currentPos.Minutes,currentPos.Seconds);
CurrentPosition.Text=currentTime;
}
});
}

“它给了我一个异常”不是一个合适的问题描述。请使用“ThreadPoolTimer.CreatePeriodicTimer(_-clockTimer_-Tick,TimeSpan.FromMillics(1000));”要创建计时器,你能帮我解决这个问题吗?
ThreadPoolTimer timer;

            // for displaying time only
            private void CurrentPlayer_MediaOpened(MediaPlayer sender, object args)
            {
                timer = ThreadPoolTimer.CreatePeriodicTimer(_clockTimer_Tick, TimeSpan.FromSeconds(1));
            }


            private async 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!
                     if (CurrentPlayer.NaturalDuration.TotalSeconds < 0)
                     {
                         TimeSpan currentPos = CurrentPlayer.Position;
                         var currentTime = string.Format("{0:00}:{1:00}", (currentPos.Hours * 60) + currentPos.Minutes, currentPos.Seconds);
                         CurrentPosition.Text = currentTime;
                     }

                 });
            }