Click here to Skip to main content
15,843,727 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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.
VB
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

VB
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

VB
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

VB.NET
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

C#
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


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.
Posted
Updated 23-Feb-16 8:12am

1 solution

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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900