Solved

WPF Run Function In New Thread

Posted on 2014-01-27
3
469 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
  • 2
3 Comments
 
LVL 83

Expert Comment

by:CodeCruiser
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
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…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

744 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now