Results 1 to 5 of 5

Thread: [RESOLVED] Seeking advice regarding seperation GUI from calculation

  1. #1

    Thread Starter
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Resolved [RESOLVED] Seeking advice regarding seperation GUI from calculation

    In my application the calculation of object-movements is presently done in the GUI-thread. Using it that way I can Invalidate the display-area of the old postion for each object, calculate the new position and then Invalidate the new area in order to speed up the Paint for the whole display.

    Since the calculation of all the object-movements could be time consuming I'd like to do that in a background thread. However I'd be loosing the above mention way to Invalidate only display-areas where a an object was and where it is after the calculation. In other words I would have to draw the complete display in each cycle.
    Does that still justify the use of a second thread or should I stay in the first mentioned procedure.

    Or am I missing something?
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  2. #2
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    Re: Seeking advice regarding seperation GUI from calculation

    You're missing something. You simply have the background calculation keep a track of areas that need invalidating, and have the UI thread access that and only invalidate those regions.

  3. #3

    Thread Starter
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: Seeking advice regarding seperation GUI from calculation

    OK, thanks.
    I do have a further question on your suggestion:
    In can calculate the regions that need to be invalidated on the BackGroundThread, however how would you hand that over to the UI thread. Save all the regions in a global Variable, report progress and in the .ProgressChanged Event use that Variable the hand the regions to the GUI??
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  4. #4
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    Re: Seeking advice regarding seperation GUI from calculation

    It would depend to a certain extent how you are handling your threading. I had assumed you were starting a new Thread to run your game loop when I read your initial description, but it sounds more like you've gone for a BackgroundWorker. In which case, yes you could try stuffing the data across in the ReportProgress event. However, I'd suggest trying for a different approach. Have a single ConcurrentQueue(Of Rectangle) which the background thread can Enqueue rectangles that need invalidating onto, and the UI thread can TryDequeue from. There is no issue with both the background thread and the UI thread handling the rectangles, because they are not UI controls and aren't owned by a thread. Once the background thread has written them to the queue it doesn't touch them again so there is no issue with mutating state (Rectangles may even be immutable?). The queue needn't be stored globally either, both the UI thread and the background thread can be quite happy with a local reference.

    How are you signalling to the UI thread that it needs updating? Presumably a ReportProgress call? That can trigger the UI thread pulling the Rectangles and invalidating those that need to be done no problem.

  5. #5

    Thread Starter
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: Seeking advice regarding seperation GUI from calculation

    Many thanks, I'll give Queue a try
    Excuse me for not telling that I'm using Backgroundworkers.
    This one will keep me busy for while, since I will encounter some cross thread problems (I started this thing when I joined in here, so I have to untangle a lot,
    Topic is solved for me.
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

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