使用调度程序计时器运行线程 [英] Run thread with dispatcher timer inside

查看:85
本文介绍了使用调度程序计时器运行线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我有一个主屏幕页面(包括视频播放和视频完成所谓的广告页面后的图像更改)。目前我的应用程序面临挂起或冻结,如果主屏幕闲置并循环超过10次。



冻结时,如果再次公开触摸屏幕,它将会变成半透明的白色与windows应用程序对话框没有响应。在对话框中,扩展细节时出现AppHangB1错误。



我目前的代码是在页面加载时,它将首先播放视频。媒体显示后,将显示图片广告,并开始调度计划。当dispatchertimer_tick时,它将变为下一个图像。图像显示结束后,它将播放媒体并继续重复相同的过程。我注意到,当应用程序处于空闲状态时,视频和图像的变化看起来很慢,触摸时会显示如上段落的对话框。



I有搜索AppHangB1问题,它说关于运行UI线程。我不知道细节,因为我还没有深入开放UI Thread。以下是我目前的代码。

Hi,

I have a page of main screen(consist of video playing and image changing after video is finish so-called advertisement page). Currently my application facing hang or freeze if the main screen is left idle and loop more than 10 times.

And while freeze, if public touch again the screen, it will become semi-transparent white with windows dialog of application is not responding. At the dialog, there is error stated "AppHangB1" when expand the details.

My current code for this is at page loaded, it will play video first. After mediaended, image ads will display and dispatchertimer will start. When dispatchertimer_tick, it will change to next image. After image show to the end, it will play back the media and keep repeat the same process. I'm noticed that while the application is left idle, the video and image changing quite look slow, and when touch, it will appear dialog like above paragraphs.

I have search for AppHangB1 problem, it said about running UI Thread. I don't know the details since I'm not open yet to UI Thread deeply. Below are my code currently.

Private Sub my_media_MediaFailed(ByVal sender As Object, ByVal e As System.Windows.ExceptionRoutedEventArgs) Handles my_media.MediaFailed
    Try
        App.LogEvents("[" & strlogpage & "] Media failed to play.", EventLogEntryType.Information)
    Catch ex As Exception
        App.LogEvents("[" & strlogpage & "] Error occurred in my_media_MediaFailed: " & ex.Message, EventLogEntryType.Error)
    End Try
End Sub




Private Sub my_media_MediaOpened(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles my_media.MediaOpened
    Try
        App.LogEvents("[" & strlogpage & "] Media opened", EventLogEntryType.Information)
        If Not (IsNothing(my_media)) Then
            my_media.Play()
        Else
            MakeInstanceAgain()
        End If
    Catch ex As Exception
        App.LogEvents("[" & strlogpage & "] Error occurred in my_media_MediaOpened: " & ex.Message, EventLogEntryType.Error)
    End Try
End Sub




Private Sub my_media_MediaEnded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles my_media.MediaEnded
    Dim imageContent As Byte()
    Try
        Me.Focus()
        If arr_ads.Count = 0 Then
            If Not (IsNothing(my_media)) Then
                If my_media.Position <> TimeSpan.Zero Then
                    my_media.Stop()
                End If
                my_media.Source = Nothing
                my_media.Source = New Uri(load_vids, UriKind.RelativeOrAbsolute)
                my_media.Position = TimeSpan.Zero
            Else
                MakeInstanceAgain()
            End If
        Else
            If Not (IsNothing(my_media)) Then
                my_media.Stop()
                my_media.Source = Nothing
            Else
                MakeInstanceAgain()
            End If
            ImagePointer = 0
            myimages.Visibility = Windows.Visibility.Visible
            myvideo.Visibility = Windows.Visibility.Hidden

            imageContent = Nothing
            imageContent = arr_ads.Item(ImagePointer)
            Dim photo As New BitmapImage()
            Dim mstream As System.IO.MemoryStream
            If Not (IsNothing(imageContent)) Then
                mstream = New System.IO.MemoryStream(imageContent)
                photo.BeginInit()
                photo.StreamSource = mstream
                photo.CacheOption = BitmapCacheOption.OnLoad
                photo.EndInit()
                img1.Source = photo
                img1.Stretch = Stretch.Fill
                mstream = Nothing
            End If
            img2.Source = Nothing
            ScreenTimer.Start()
        End If
    Catch ex As Exception
        App.LogEvents("[" & strlogpage & "] Error occurred In MediaAdvert_MediaEnded: " & ex.ToString, EventLogEntryType.Error)
    End Try
End Sub




Private Sub ScreenTimer_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles ScreenTimer.Tick
        Dim s As Storyboard
        Dim imageContent As Byte()
        Try
            Me.IsEnabled = False
            ScreenTimer.Stop()
            If Not (IsNothing(arr_ads)) Then
                If arr_ads.Count > 0 Then
                    If ImagePointer = arr_ads.Count - 1 Then
                        ImagePointer = 0
                        If load_vids = String.Empty Then
                            imageContent = Nothing
                            imageContent = arr_ads.Item(ImagePointer - 1)
                            Dim photo As New BitmapImage()
                            Dim mstream As System.IO.MemoryStream
                            If Not (IsNothing(imageContent)) Then
                                mstream = New System.IO.MemoryStream(imageContent)
                                photo.BeginInit()
                                photo.StreamSource = mstream
                                photo.CacheOption = BitmapCacheOption.OnLoad
                                photo.EndInit()
                                img1.Source = photo
                                img1.Stretch = Stretch.Fill
                                mstream = Nothing
                            End If
                            imageContent = Nothing
                            imageContent = arr_ads.Item(ImagePointer)
                            Dim photo2 As New BitmapImage()
                            Dim mstream2 As System.IO.MemoryStream
                            If Not (IsNothing(imageContent)) Then
                                mstream2 = New System.IO.MemoryStream(imageContent)
                                photo2.BeginInit()
                                photo2.StreamSource = mstream2
                                photo2.CacheOption = BitmapCacheOption.OnLoad
                                photo2.EndInit()
                                img2.Source = photo2
                                img2.Stretch = Stretch.Fill
                                mstream = Nothing
                            End If
                            Me.IsEnabled = True
                            ScreenTimer.Start()
                        Else
                            ScreenTimer.Stop()
                            MediaPointer = 0
                            myvideo.Visibility = Windows.Visibility.Visible
                            myimages.Visibility = Windows.Visibility.Hidden
                            If Not (IsNothing(my_media)) Then
                                my_media.Source = Nothing
                                my_media.Source = New Uri(load_vids, UriKind.RelativeOrAbsolute)
                                my_media.Position = TimeSpan.Zero
                            Else
                                MakeInstanceAgain()
                            End If
                            Me.IsEnabled = True
                        End If
                    Else
                        imageContent = Nothing
                        imageContent = arr_ads.Item(ImagePointer)
                        Dim photo As New BitmapImage()
                        Dim mstream As System.IO.MemoryStream
                        If Not (IsNothing(imageContent)) Then
                            mstream = New System.IO.MemoryStream(imageContent)
                            photo.BeginInit()
                            photo.StreamSource = mstream
                            photo.CacheOption = BitmapCacheOption.OnLoad
                            photo.EndInit()
                            img1.Source = photo
                            img1.Stretch = Stretch.Fill
                            mstream = Nothing
                        End If
                        ImagePointer = ImagePointer + 1
                        imageContent = Nothing
                        imageContent = arr_ads.Item(ImagePointer)
                        Dim photo2 As New BitmapImage()
                        Dim mstream2 As System.IO.MemoryStream
                        If Not (IsNothing(imageContent)) Then
                            mstream2 = New System.IO.MemoryStream(imageContent)
                            photo2.BeginInit()
                            photo2.StreamSource = mstream2
                            photo2.CacheOption = BitmapCacheOption.OnLoad
                            photo2.EndInit()
                            img2.Source = photo2
                            img2.Stretch = Stretch.Fill
                            mstream = Nothing
                        End If
                        Me.IsEnabled = True
                        ScreenTimer.Start()
                    End If
                Else
                    ScreenTimer.Stop()
                End If
            End If
        Catch ex As Exception
            App.LogEvents("[" & strlogpage & "] Error occurred at screen timer tick: " & ex.ToString, EventLogEntryType.Error)
            ScreenTimer.Start()
        End Try
    End Sub




Private Sub btn_touch_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles btn_touch.Click
    Try
        Me.IsEnabled = False
        ScreenTimer.Stop()
        If Not (IsNothing(my_media)) Then
            my_media.Stop()
            my_media.Position = TimeSpan.Zero
            my_media.Close()
            my_media.Source = Nothing
        End If
        App.LogEvents("[" & strlogpage & "] Touch detected. Navigating to welcome page.", EventLogEntryType.Information)
        Dim pagename As New PageWelcome
        NavigationService.Navigate(pagename)
    Catch ex As Exception
        strMsg = "[" & strlogpage & "] Error at touchdown navigate. Ex:" & ex.Message
        App.LogEvents(strMsg, EventLogEntryType.Error)
    End Try
End Sub





问题: -

我可以运行一个backgroundworker / dispatcher /其中的任何线程,它会启动一个timer / dispatchertimer并继续在线程内重复它所以UI是打开以获得任何响应,例如触摸,所以当公众触摸屏幕时,我只发送CancelAsync。有什么想法吗?



我尝试过:



Application.Current .Dispatcher.BeginInvoke(DispatcherPriority.Background,New Action(Function()Me.progressBar.Value = 50))作为样本从讨论和论坛中获取,但因为我不知道如何继续而挂在那里。



QUESTION:-
Can I run a backgroundworker/dispatcher/any thread which inside it, it will start a timer/dispatchertimer and keep repeating it inside thread so UI is open to have any response such as touch so when public is touching the screen, I just send the CancelAsync. Any idea?

What I have tried:

Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Background, New Action(Function() Me.progressBar.Value = 50)) get from discussion and forum as sample but hang there because I don't know how to proceed.

推荐答案

一个计时器事件总是开始进入自己的线程 - 认为这是你遇到的基本问题(线程中的线程)。而不是计时器尝试使用无限的循环执行 sleep ,并在开始或结束时使用所需的间隔环。也可以在循环中使用 try / catch块来拦截线程中止时引发的特殊错误。
A timer event always starts into own thread - think this is the basic problem you ran into (thread in thread). Instead of the timer try to use an endless loop that does a sleep with the desired interval at start or end of the loop. Also use try/catch block inside the loop to intercept the special error that is raised when the the thread is aborted.


这篇关于使用调度程序计时器运行线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆