Solved

WPF Run Function In New Thread

Posted on 2014-01-27
3
481 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
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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

735 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