[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?
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?
Re: Weird behaviour of variable scope using BackgroundWorker
No,it doesn't
vb Code:
Public ReadOnly Property KartenHöhe_ As Integer
Get
Return Me.KartenHöhe
End Get
End Property
elsewhere, before the BackgroundWorker starts
vb Code:
KartenHöhe=PictureBox.Height
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.
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.
Re: Weird behaviour of variable scope using BackgroundWorker
I was thinking that maybe the types aren't thread safe, but Sytem.Int32 clearly is:
Quote:
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
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.