Results 1 to 7 of 7

Thread: [RESOLVED] Weird behaviour of variable scope using BackgroundWorker

  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] Weird behaviour of variable scope using BackgroundWorker

    I changed the scope of some Global variables to be private of a form, and added a readonly property for them.
    From within the form a backgroundworker gets started which uses routines from a specific module (calculating my backgroundimage).
    The above mentioned variables are need, so I try to read them in by the readonly properties, which only works for one of them????
    The that it is working has no direct realtion to any control, while the other are copies of the pictureboxes-properties where the image is going to added (Width and Heigth).
    My workaround was to handover the values into _DoWork Sub.
    Cold anybody tell why it didn't work for all the properties as intended?
    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
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109

    Re: Weird behaviour of variable scope using BackgroundWorker

    When you say that the others are copies of picturebox properties, does the Get portion reference the control itself?
    My usual boring signature: Nothing

  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: Weird behaviour of variable scope using BackgroundWorker

    No,it doesn't
    vb Code:
    1. Public ReadOnly Property KartenHöhe_ As Integer
    2.         Get
    3.             Return Me.KartenHöhe
    4.         End Get
    5.     End Property
    elsewhere, before the BackgroundWorker starts
    vb Code:
    1. KartenHöhe=PictureBox.Height
    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
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109

    Re: Weird behaviour of variable scope using BackgroundWorker

    What I was first thinking when I saw this was that this was the startup form, in which case it is probably the default instance. Default instances of forms are thread specific, which could be the cause of the problem. However, that would make more sense if NONE of the properties worked. That explanation doesn't explain why one of them works.
    My usual boring signature: Nothing

  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: Weird behaviour of variable scope using BackgroundWorker

    It is the StartUp Form. I woudn't even have asked if none of them had worked.
    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!

  6. #6
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,397

    Re: Weird behaviour of variable scope using BackgroundWorker

    I was thinking that maybe the types aren't thread safe, but Sytem.Int32 clearly is:
    All members of this type are thread safe. Members that appear to modify instance state actually return a new instance initialized with the new value. As with any other type, reading and writing to a shared variable that contains an instance of this type must be protected by a lock to guarantee thread safety.
    Have you tried setting the default instance of KartenHöhe ie-
    Code:
    Private KartenHöhe As Integer = -1
        Public ReadOnly Property KartenHöhe_ As Integer
            Get
                Return Me.KartenHöhe
            End Get
        End Property
    Instead of:
    [code]
    Code:
    Private KartenHöhe As Integer
        Public ReadOnly Property KartenHöhe_ As Integer
            Get
                Return Me.KartenHöhe
            End Get
        End Property
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  7. #7

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

    Re: Weird behaviour of variable scope using BackgroundWorker

    Tried it, and the default values show. So it is not the default instance(which I am using) on which the properties are looking at. The one value which was "working", had a default value set and I didn't realise that. So all properties read show the same behaviour.

    Thanks, for that.

    BTW: I have already my workaround, as posted earlier. I was just interested in the reason.
    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