Solved

WPF Run Function In New Thread

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

813 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

12 Experts available now in Live!

Get 1:1 Help Now