Saturday, 14 September 2013

Is BackgroundWorker.RunWorkerCompleted Thread Safe

Is BackgroundWorker.RunWorkerCompleted Thread Safe

Have a problem that is very hard to reproduce.
The exception is:
e.Exception.Message = Object reference not set to an instance of an object.
e.Exception.StackTrace
at
System.Windows.Controls.ItemContainerGenerator.MoveToPosition(GeneratorPosition
position, GeneratorDirection direction, Boolean allowStartAtRealizedItem,
GeneratorState& state)
at
System.Windows.Controls.ItemContainerGenerator.Generator..ctor(ItemContainerGenerator
factory, GeneratorPosition position, GeneratorDirection direction, Boolean
allowStartAtRealizedItem)
I catch that in App.cs App_DispatcherUnhandledException
I think that is from XAML as I have all the Break on Thrown turned on very
every option in Visual Studio
And I am pretty sure I have all the code behind wrapped in Try Catch
For sure the page where I think the error is originating is catching all
code behind exceptions.
And if I don't have the Tab visible so the XAML will not see a
NotifyChanged I cannot get error to happen.
If I break on the get for those public properties I cannot get the error
to happen.
As I slow it down to try and debug then it is harder to reproduce.
Have some tabs where I display text using a TextBlock and formatted text
using FlowDocumentViewer.
Source is bound to public properties. The public property for the
TextBlock.Text is RawText and the backing variable is rawText.
I get the text and information to highlight the text on a BackgroundWorker
Before calling the BackgroundWorker I set the rawText to "getting text"
call CancelAsync().
Then on RunWorkerCompletedEventHandler I set the rawText to the actual
text. Then I only call notifypropertychanged if that tab is selected so it
does not render if it is not visible.
Could the problem be that I am changing the rawText in the
RunWorkerCompletedEventHandler while the control is reading the rawText?
Or I am changing the rawText to "getting text" while the UI control is
reading the text? Reaching but I have worked on this for 3 days and that
is all I can figure.
And other thoughts?
Would placing locks on those assignments fix it?
Not MVVM.

No comments:

Post a Comment