chris_asa
Active member
I'm using a BackgroundWorker and passing a class° as argument. BackgroundWorker arguments are perforce ByVal. In principle, this leaves me free to play with the original class in the UI thread without hurting the ByVal "clone" being processed by the BackgroundWorker.
BUT!
My experiments suggest that the ByVal class argument is not "materialized" until the BackgroundWorker actually reads the args.
EXAMPLE if BackgroundWorker has Thread.Sleep(2000) BEFORE reading the args (ByVal class) AND the UI thread modifies its instance of the class during that 2 seconds, the BackgroundWorker instance of the class IS MODIFIED when the args are eventually collected.
The first thing I want to do in the UI thread after spawning the BackgroundWorker is to clear the original class instance. On a busy machine, I can imagine a situation where the BackgroundWorker takes some time to "get off the ground". It would be an unimaginable disaster for my application if clearing the UI-instance of the class destroyed the data that the BackgroundWorker was supposed to handle.
A possible workaround is for the Backgroundworker to UpdateProgress after it has read the args, and for the UI thread to wait for this progress update before it clears the class.
Are my experiments flawed ?
or is the ByVal argument really only "materialized" after reading the args ?
or (worse for my intended use) is the supposed ByVal argument not truly ByVal at all ?
If ByVal actually is ByVal but only when the args are read, is there a better workaround than the progress update ?
regards Chris
° Probably not relevant, but my class is a parent class containing 5 child classes. Two of the child classes contain a datatable. Overall it can be quite a "heavy" object: one of the datatables has typically 10...100 rows - 32k rows are allowed, but never reached in reality.
BUT!
My experiments suggest that the ByVal class argument is not "materialized" until the BackgroundWorker actually reads the args.
EXAMPLE if BackgroundWorker has Thread.Sleep(2000) BEFORE reading the args (ByVal class) AND the UI thread modifies its instance of the class during that 2 seconds, the BackgroundWorker instance of the class IS MODIFIED when the args are eventually collected.
The first thing I want to do in the UI thread after spawning the BackgroundWorker is to clear the original class instance. On a busy machine, I can imagine a situation where the BackgroundWorker takes some time to "get off the ground". It would be an unimaginable disaster for my application if clearing the UI-instance of the class destroyed the data that the BackgroundWorker was supposed to handle.
A possible workaround is for the Backgroundworker to UpdateProgress after it has read the args, and for the UI thread to wait for this progress update before it clears the class.
Are my experiments flawed ?
or is the ByVal argument really only "materialized" after reading the args ?
or (worse for my intended use) is the supposed ByVal argument not truly ByVal at all ?
If ByVal actually is ByVal but only when the args are read, is there a better workaround than the progress update ?
regards Chris
° Probably not relevant, but my class is a parent class containing 5 child classes. Two of the child classes contain a datatable. Overall it can be quite a "heavy" object: one of the datatables has typically 10...100 rows - 32k rows are allowed, but never reached in reality.
Last edited: