First run the Code:
[Csharp]
Public partial class MainWindow: Window
{
Public MainWindow ()
{
InitializeComponent ();
Thread t = new Thread (new ParameterizedThreadStart (threadTest ));
T. Start (@ "C: \ Users \ Public \ Pictures \ Sample Pictures \ Chrysanthemum.jpg ");
}
Void threadTest (object obj)
{
String path = obj as string;
This. Dispatcher. Invoke (new Func <object> () => this. Background = new ImageBrush (new BitmapImage (new Uri (path )))));
}
}
The above code can be correctly run, but the following code is incorrect:
[Html]
Public partial class MainWindow: Window
{
Public MainWindow ()
{
InitializeComponent ();
Thread t = new Thread (new ParameterizedThreadStart (threadTest ));
T. Start (@ "C: \ Users \ Public \ Pictures \ Sample Pictures \ Chrysanthemum.jpg ");
}
Void threadTest (object obj)
{
String path = obj as string;
// This. Dispatcher. Invoke (new Func <object> () => this. Background = new ImageBrush (new BitmapImage (new Uri (path )))));
ImageBrush background = new ImageBrush (new BitmapImage (new Uri (path )));
This. Dispatcher. Invoke (new Func <object> () => this. Background = background ));
}
}
The only difference between the two codes is that the previous Code creates an ImageBrush object in the main thread, and the other Code creates an ImageBrush object in the Child thread.
In WPF, can all threads only call objects created by this thread?
From I have a adream