无法在两个线程中同时使用Window控件

时间:2013-08-03 09:16:56

标签: wpf vb.net multithreading visual-studio-2008 screenshot

从Window1的Page_load事件中,我调用公共类的函数并将参数作为相同的Window1传递。调用该函数后,将启动一个线程。在Window1的Page_Loaded事件上调用该线程。代码是这样的:

Private Sub StartScreen_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        Try
            TmHeartbeat.Stop()
            TmHeartbeat.Interval = 600000
            TmHeartbeat.Start()
            ResetVariables()
            FormLoadSetting(Me)
            SetButtonProperty(btnEnglish, "\Images\ButtonBgBig.png", GetFormLabel("Start Screen", "Common", "Button English"))
            SetButtonProperty(btnSpanish, "\Images\ButtonBgBig.png", GetFormLabel("Start Screen", "Common", "Button Spanish"))
            SetDisplayTimer(DGT, False, Me, 1800000)

            MediaElement1.Source = New Uri(GetPath("Images\EnglishVideo\" & GetFormLabel("Start Screen", "Common", "Video")))

            If GetFormLabel("Start Screen", "Common", "Audio") <> "" Then
                PlayAudio(APlay, GetFormLabel("Start Screen", "Common", "Audio"))
            End If

            Dim AudioPlay As New System.Media.SoundPlayer

            Dim sc As New Screenshot
            sc.TakeScreenshot(Me)
        Catch ex As Exception
            AliLogFileEntry(TransactionType.ErrorOnForm, "Error In Function: StartScreen_Loaded: " & Me.Title & ", ErrorMessage: " & ex.Message)
        End Try
    End Sub

调用类TakeScreenshot(Me)中的函数Screenshot。除了Screenshot类,我还有另一个名为GetScreenshot的类和一个函数TakeScreenshot1。类文件的代码如下:

Imports System.IO
Imports System.Threading
Public Class Screenshot
    Public Sub TakeScreenshot(ByVal formname As Window)
        Dim GT As New GetScreenshot
        GT.source = formname
        Dim newThread As New Thread(AddressOf GT.TakeScreenshot1)
        newThread.Start()
    End Sub

End Class

Public Class GetScreenshot
    Public source As Window
    Public Function TakeScreenshot1()
        Thread.Sleep(2000)
        If OG.GetValue("TakeScreenshot") <> "0" Then
            Try
                AliLogFileEntry(TransactionType.System, "In Function: TakeScreenshot")
                Dim scale As Double = OG.GetValue("Screenshot_Scale") / 100
                AliLogFileEntry(TransactionType.System, "In Function: GetJpgImage")
                Dim renderHeight As Double = source.RenderSize.Height * scale
                Dim renderWidth As Double = source.RenderSize.Width * scale
                Dim renderTarget As New RenderTargetBitmap(CInt(Math.Truncate(renderWidth)), CInt(Math.Truncate(renderHeight)), 96, 96, PixelFormats.Pbgra32)
                Dim sourceBrush As New VisualBrush(source)
                Dim drawingVisual As New DrawingVisual()
                Dim drawingContext As DrawingContext = drawingVisual.RenderOpen()
                Using drawingContext
                    drawingContext.PushTransform(New ScaleTransform(scale, scale))
                    drawingContext.DrawRectangle(sourceBrush, Nothing, New Rect(New Point(0, 0), New Point(source.RenderSize.Width, source.RenderSize.Height)))
                End Using
                renderTarget.Render(drawingVisual)
                Dim jpgEncoder As New JpegBitmapEncoder()
                jpgEncoder.QualityLevel = 100

                jpgEncoder.Frames.Add(BitmapFrame.Create(renderTarget))
                Dim _imageArray As [Byte]()
                Using outputStream As New MemoryStream()
                    jpgEncoder.Save(outputStream)
                    _imageArray = outputStream.ToArray()
                End Using
                Dim screenshot As Byte() = _imageArray
                Dim dir As DirectoryInfo = New DirectoryInfo("Screenshots")
                If Not dir.Exists Then dir = Directory.CreateDirectory(dir.FullName)
                Dim path As String = AppDomain.CurrentDomain.BaseDirectory
                Dim fileStream As New IO.FileStream("Screenshots\" & source.Title & ".jpg", FileMode.Create, FileAccess.ReadWrite)
                Dim binaryWriter As New IO.BinaryWriter(fileStream)
                binaryWriter.Write(screenshot)
                binaryWriter.Close()
            Catch ex As Exception
                AliLogFileEntry(TransactionType.ErrorOnForm, "Error In Function: TakeScreenshot , ErrorMessage: " & ex.Message)
            End Try
        End If
    End Function
End Class

当我调试此文件时,出现此错误: enter image description here

错误是在线上生成的

Dim sourceBrush As New VisualBrush(source)

请求帮助

1 个答案:

答案 0 :(得分:3)

您使用的source来自与创建时不同的线程。为了在不同的线程上使用Windows控件,您需要回调原始线程。 有关详情,请参阅How to: Make Thread-Safe Calls to Windows Forms Controls