dcsimg
Results 1 to 6 of 6

Thread: VB.NET Cross-thread Operation not valid. Using Background Worker

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2019
    Posts
    1

    VB.NET Cross-thread Operation not valid. Using Background Worker

    I am very new to vb and i'm trying to create pacman for a school project. Basically, I have the 4 ghost moving randomly in 4 separate background workers. But when I run it I get an error for the picturebox of the first ghost located in the first background worker.
    Error:
    Code:
    System.InvalidOperationException: 'Cross-thread operation not valid: Control 'picBlinky' accessed from a thread other than the thread it was created on.'
    Structure of my code:
    In async form1 load:
    BackgroundWorker1.runworkasync()
    await then repeat 3 other times for the other background worker
    Then heres the first part of backgroundworker1 where the error occurs
    Code:
    Private Async Sub BackgroundWorker1_DoWork(sender As Object, e As DoWorkEventArgs) Handles BackgroundWorker1.DoWork
    Do
    If picBlinky.Location = PictureBox124.Location Then
                    For counterUP As Integer = 1 To 40
                        picBlinky.Location = New Point(picBlinky.Location.X, picBlinky.Location.Y - 1)            <- this line is the error
                        Await Task.Delay(0.55) 'delay for each movement
                        picBlinky.BringToFront()
                        If picBlinky.Bounds.IntersectsWith(picPacMan.Bounds) And Not picPacMan.Bounds.IntersectsWith(picBlinky.Bounds) And spooky = False Then
                            lives -= 1
    
    'repeat a similar format for all other turning option where if there is more than one, it would randomize into a select case
    x4

    I tried searching it up and if i turn off illegal cross thread check, it would work but it caused quite a few bugs that don't always happen but didn't exist before. So I read about BackgroundWorker.Report Progress and ProgressChanged, but after some research i still have no idea how it works (terms were confusings) and how to apply to my form since the examples i have seen are all realated to labels and numbers

    I would appreciate any help I can get Thanks. If you want to see the entire thing, just tell me and I'll attach it but it is very messed up and unorganized since I'm very new to VB. Thanks again!

  2. #2
    Lively Member
    Join Date
    Jul 2017
    Location
    Exeter, UK
    Posts
    67

    Re: VB.NET Cross-thread Operation not valid. Using Background Worker

    You need to understand that your UI, i.e. all the controls you interact with e.g. buttons, forms, picture box's etc. are using one main thread to handle any changes. When you create a background worker it operates on a NEW separate thread and cannot interact with the main directly unless you use a delegate. Have a read up on these to see how they work.

  3. #3
    Lively Member
    Join Date
    Jun 2018
    Posts
    85

    Re: VB.NET Cross-thread Operation not valid. Using Background Worker

    Well you certainly are ambitious.

    Hmmm. I dont think you should try to use the background workers or threading for now.

    If you use a Windows Forms Timer Control it will allow you to run in a game loop that does not freeze up the app controls etc when done right.

    So what is the the basic game loop? Something like (not real code):

    Code:
        Do
    
           app does this and that
    
        Loop

    but if you run that loop, as you know, your app never returns to the system to do the waiting system events like process other apps and your app controls etc.

    However if you use a timer you get a loop like this at system level:

    Code:
        do 
            if lasttime + time interval > this time then
    
                 Fire timer event sub
                      
                                                  app does this and that
    
                                         end sub
            end if
    
            Process all other system tasks
    
        loop

    So IMHO you should just use a timer control for now until you learn more.

    I have done some fairly complex graphics game loops with a timer and it works well. Not that complicated. Then you can spend your time learning to draw and animate more.

    We can show you how to make your own async loop timer similar to what you are doing in your bgw using the form load event that will be more acurate but I still dont think you need it yet.

    Do yourself a favor and use a loop run with ONE windows form timer control. YOu can use counters to create the game logic. Then you will start to enjoy programing again as you can work on whats fun and your game will actually run.

    Do all your drawing at once dont try to thread the sprites. Draw the entire screen at once in the timer event then exit to system.

    Too much to say...

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    100,984

    Re: VB.NET Cross-thread Operation not valid. Using Background Worker

    The point of a BackgroundWorker is to perform background work. Anything to do with the UI is the very opposite of background work. The UI is the foreground of the application. The idea with a BackgroundWorker is that you perform calculations or the like in the DoWork even handler and if you need to update the UI then you can call ReportProgress and handle the ProgressChanged event along the way and/or handle the RunWorkerCompleted event when the work is done. You could possibly have a loop in the DoWork event handler and call ReportProgress on each iteration and then update the UI in the ProgressChanged event handler but I doubt that that is the best option. It seems more likely that using a Timer and handling the Tick event would be a better option.

  5. #5
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    100,984

    Re: VB.NET Cross-thread Operation not valid. Using Background Worker

    Quote Originally Posted by tommytwotrain View Post
    If you use a Windows Forms Timer Control it will allow you to run in a game loop that does not freeze up the app controls etc when done right.
    Just a small and relatively inconsequential point but the Timer is not a control. It is a component, i.e. it inherits the Component class, which is all that's required to be used in the WinForms designer, but it is not a control, i.e. it does not inherit the Control class.

  6. #6
    Lively Member
    Join Date
    Jun 2018
    Posts
    85

    Re: VB.NET Cross-thread Operation not valid. Using Background Worker

    Quote Originally Posted by jmcilhinney View Post
    Just a small and relatively inconsequential point but the Timer is not a control. It is a component, i.e. it inherits the Component class, which is all that's required to be used in the WinForms designer, but it is not a control, i.e. it does not inherit the Control class.
    Jmc,

    Oh, I see. I should have realized that.


Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width