Solved

WPF Run Function In New Thread

Posted on 2014-01-27
3
484 Views
Last Modified: 2014-03-03
So I have a WPF app that takes a screenshot every 30 seconds.  When this happens the application hangs for about half a second.  I am wanting to move this function to it's own thread to prevent this, but I am very confused on how to do this.  I have been Googling for the past two days to no avail.  Thanks for any help.

Here is my code that takes the screenshot:
 Private Sub TakeScreenshot_Click()
        Dim theval As Double = 1

        Dim screenshot As Byte() = GetJpgImage(Me, theval, 100)
        Dim fileStream As New FileStream("c:\MyPic.jpg", FileMode.Create, FileAccess.ReadWrite)
        Dim binaryWriter As New BinaryWriter(fileStream)
        binaryWriter.Write(screenshot)
        binaryWriter.Close()
    End Sub

    Public Shared Function GetJpgImage(source As UIElement, scale As Double, quality As Integer) As Byte()
        Dim actualHeight As Double = source.RenderSize.Height
        Dim actualWidth As Double = source.RenderSize.Width
        Dim renderHeight As Double = actualHeight * scale
        Dim renderWidth As Double = actualWidth * scale
        Dim renderTarget As New RenderTargetBitmap(CInt(renderWidth), CInt(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(actualWidth, actualHeight)))
        End Using
        renderTarget.Render(drawingVisual)
        Dim jpgEncoder As New JpegBitmapEncoder()
        jpgEncoder.QualityLevel = quality
        jpgEncoder.Frames.Add(BitmapFrame.Create(renderTarget))
        Dim _imageArray As [Byte]()
        Using outputStream As New MemoryStream()
            jpgEncoder.Save(outputStream)
            _imageArray = outputStream.ToArray()
        End Using
        Return _imageArray
    End Function

Open in new window


I call it using:

TakeScreenshot_Click()

Open in new window


I think it's the above call that I want to move to a thread.  Anyway, thanks for any help on this.
0
Comment
Question by:G Scott
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 39814766
Is TakeScreenshot_Click the button handler? If it is then you need to run GetJpgImage on separate thread. But you may get an error as you are accessing UI which is not allowed on any thread other than UI thread.

You can try following

Change TakeScreenshot_Click() to following


Private Sub TakeScreenshot_Click()
        Dim th As New Threading.Thread(Addressof GetJpgImage)
        th.Start()
    End Sub

Open in new window



Change GetJpgImage to following

Public Shared Function GetJpgImage(source As UIElement, scale As Double, quality As Integer) As Byte()
        Dim actualHeight As Double = source.RenderSize.Height
        Dim actualWidth As Double = source.RenderSize.Width
        Dim renderHeight As Double = actualHeight * scale
        Dim renderWidth As Double = actualWidth * scale
        Dim renderTarget As New RenderTargetBitmap(CInt(renderWidth), CInt(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(actualWidth, actualHeight)))
        End Using
        renderTarget.Render(drawingVisual)
        Dim jpgEncoder As New JpegBitmapEncoder()
        jpgEncoder.QualityLevel = quality
        jpgEncoder.Frames.Add(BitmapFrame.Create(renderTarget))
        Dim _imageArray As [Byte]()
        Using outputStream As New MemoryStream()
            jpgEncoder.Save(outputStream)
            _imageArray = outputStream.ToArray()
        End Using
        Dim fileStream As New FileStream("c:\MyPic.jpg", FileMode.Create, FileAccess.ReadWrite)
        Dim binaryWriter As New BinaryWriter(fileStream)
        binaryWriter.Write(_imageArray)
        binaryWriter.Close()
    End Function

Open in new window

0
 
LVL 1

Author Comment

by:G Scott
ID: 39814894
Ha, TakeScreenshot_Click() isn't a button click.  It was some code I found that was attached to a button, but I am not using it that way.

So I have a couple questions.  First, intellisense does not like the 'TakeScreenshot' code:
ErrorI tried a few different variations on this and it kept complaining.  Both System.Threading and System.Windows.Threading don't work.  Also, with the rewritten function, how do I pass the arguments?  I pass (Me, 1, 100) to take the pic.  How would I do that now?

Thanks for your help on this CC.
0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 500 total points
ID: 39814945
One easy approach for passing arguments is to declare and use class level variables.

Probably threading is different in WPF. Try creating a ThreadStart object

Dim ts As New System.Threading.ThreadStart(AddressOf GetJpgImage)

then supply this to thread

Dim th as New System.Threading.Thread(ts)
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question