Results 1 to 3 of 3

Thread: Problem rendering graphics with a BackgroundWorker

Threaded View

  1. #1

    Thread Starter
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,201

    Problem rendering graphics with a BackgroundWorker

    I am trying to make a graphics program with mousewheel-controlled zooming using GDI+. My plan is to provide rapid feedback, such as a low resolution render or even just a bounding rectangle, followed by a full resolution render as soon as possible afterwards. It seems logical to me to try a BackgroundWorker for the latter task. This is my first attempt to use one of these, or any other way of multithreading for that matter.

    Mousewheel events can follow one another much more quickly than I can render to the screen. That means I need to kill any rendering process that is already running and start a new one with the latest value of the zoom scaling factor. Here is a stripped down version of my code.
    vb.net Code:
    1. Imports System.ComponentModel
    2.  
    3. Public Class ZoomWorker
    4.  
    5.     Public Event ProvideFeedBack()
    6.     Public Event ScaledImageReady()
    7.     Public SourceImage As Bitmap
    8.     Public ScaledImage As Bitmap
    9.     Private _ScaleFactor As Single = 1.0
    10.  
    11.     Private WithEvents ScaleImageWorker As New BackgroundWorker
    12.  
    13.     Public Sub New()
    14.         ScaleImageWorker.WorkerSupportsCancellation = True
    15.         AddHandler DrawingSurface.MouseWheelTurned, AddressOf SetScale
    16.     End Sub
    17.  
    18.     Private Sub SetScale(ByVal sender As DrawingSurface, ByVal e As MouseEventArgs)
    19.  
    20.         'calculate the new scale factor
    21.         _ScaleFactor *= CSng(1 + e.Delta * (1 + e.Delta / 120) / 20000)
    22.         RaiseEvent ProvideFeedBack()
    23.  
    24.         If ScaleImageWorker.IsBusy Then
    25.             ScaleImageWorker.CancelAsync()
    26.  
    27.             'this is where it hangs:
    28.             Do Until ScaleImageWorker.CancellationPending = False
    29.                 Threading.Thread.Sleep(10)
    30.             Loop
    31.         End If
    32.         ScaleImageWorker.RunWorkerAsync(_ScaleFactor)
    33.  
    34.     End Sub
    35.  
    36.     Private Sub ScaleImageWorker_DoWork(ByVal sender As Object, ByVal e As DoWorkEventArgs) Handles ScaleImageWorker.DoWork
    37.         If e.Cancel Then Exit Sub
    38.         Dim worker As BackgroundWorker = CType(sender, BackgroundWorker)
    39.         e.Result = ScaleImage(CSng(e.Argument), ScaleImageWorker, e)
    40.     End Sub
    41.  
    42.     Private Sub ScaleImageWorker_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles ScaleImageWorker.RunWorkerCompleted
    43.         ScaledImage = DirectCast(e.Result, Bitmap)
    44.         RaiseEvent ScaledImageReady()
    45.     End Sub
    46.  
    47.     Private Function ScaleImage(ByVal scalefactor As Single, _
    48.             ByVal worker As BackgroundWorker, ByVal e As DoWorkEventArgs) As Bitmap
    49.         If worker.CancellationPending Then
    50.             e.Cancel = True
    51.         Else
    52.             'render and return the scaled bitmap
    53.         End If
    54.     End Function
    The above class coexists with a DrawingSurface class, a UserControl which listens for the ProvideFeedback and ScaledImageReady events and paints the image accordingly. It also raises mouse/keyboard events for the zoom and other tools to deal with.

    My problem arises in the DO loop waiting for Cancellation to finish. The zooming routine works for one or maybe half a dozen renders, depending on the image size, but then it hangs in that loop and never comes out. Can anyone advise me how to fix it?

    Of course, if there's a better alternative approach I'd also be glad to hear about it. I'm not yet ready to take on WPF, unfortunately.

    cheers, BB
    Last edited by boops boops; Apr 30th, 2009 at 06:44 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width