Solved

WPF Run Function In New Thread

Posted on 2014-01-27
3
489 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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone 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

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

717 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