请高手帮忙,不胜感激!
代码如下:
using System.Windows.Threading;
using System.Threading;
namespace wpfApplication1
{
public partial class MainWindow : Window
{
public delegate void delegate1();
public MainWindow()
{
InitializeComponent();
}
public void RunImage()
{
BitmapImage sample1 = new BitmapImage();
sample1 = new BitmapImage(new Uri(System.AppDomain.CurrentDomain.BaseDirectory
+ "..\\..\\Images\\1.png", UriKind.Absolute));
this.image1.Source = sample1;
Thread.Sleep(200);
sample1 = new BitmapImage(new Uri(System.AppDomain.CurrentDomain.BaseDirectory
+ "..\\..\\Images\\2.png", UriKind.Absolute));
this.image1.Source = sample1;
Thread.Sleep(200);
sample1 = new BitmapImage(new Uri(System.AppDomain.CurrentDomain.BaseDirectory
+ "..\\..\\Images\\3.png", UriKind.Absolute));
this.image1.Source = sample1;
Thread.Sleep(100);
sample1 = new BitmapImage(new Uri(System.AppDomain.CurrentDomain.BaseDirectory
+ "..\\..\\Images\\4.png", UriKind.Absolute));
this.image1.Source = sample1;
Thread.Sleep(200);
sample1 = new BitmapImage(new Uri(System.AppDomain.CurrentDomain.BaseDirectory
+ "..\\..\\Images\\5.png", UriKind.Absolute));
this.image1.Source = sample1;
Thread.Sleep(300);
sample1 = new BitmapImage(new Uri(System.AppDomain.CurrentDomain.BaseDirectory
+ "..\\..\\Images\\6.png", UriKind.Absolute));
this.image1.Source = sample1;
Thread.Sleep(100);
sample1 = new BitmapImage(new Uri(System.AppDomain.CurrentDomain.BaseDirectory
+ "..\\..\\Images\\7.png", UriKind.Absolute));
this.image1.Source = sample1;
Thread.Sleep(1000);
}
private void button1_Click(object sender, RoutedEventArgs e)
{
Dispatcher.BeginInvoke(DispatcherPriority.Normal, new delegate1(RunImage));
}
}
}
7 个解决方案
#1
#2
#3
#4
有线程资源保护,需要使用System.Threading.SynchronizationContext 或者 Dispatcher.BeginInvoke 类来将信息透到主线程。
SynchronizationContext sc = SynchronizationContext.Current;
通过 sc.Post(xx); 方法可以把子线程 资源传到 UI线程.
SynchronizationContext sc = SynchronizationContext.Current;
通过 sc.Post(xx); 方法可以把子线程 资源传到 UI线程.
#5
System.Threading.Thread thread = new System.Threading.Thread(
new System.Threading.ThreadStart(
delegate()
{
this.Dispatcher.Invoke(
System.Windows.Threading.DispatcherPriority.SystemIdle,
TimeSpan.FromSeconds(1),
new Action(
delegate()
{
RunImage();
}
));
}
));
thread.Start();
new System.Threading.ThreadStart(
delegate()
{
this.Dispatcher.Invoke(
System.Windows.Threading.DispatcherPriority.SystemIdle,
TimeSpan.FromSeconds(1),
new Action(
delegate()
{
RunImage();
}
));
}
));
thread.Start();
#6
BeginInvoke和 Invoke方法所调用的委托都是在 UI 线程中执行的。
支持楼上的
支持楼上的
#7
#1
#2
#3
#4
有线程资源保护,需要使用System.Threading.SynchronizationContext 或者 Dispatcher.BeginInvoke 类来将信息透到主线程。
SynchronizationContext sc = SynchronizationContext.Current;
通过 sc.Post(xx); 方法可以把子线程 资源传到 UI线程.
SynchronizationContext sc = SynchronizationContext.Current;
通过 sc.Post(xx); 方法可以把子线程 资源传到 UI线程.
#5
System.Threading.Thread thread = new System.Threading.Thread(
new System.Threading.ThreadStart(
delegate()
{
this.Dispatcher.Invoke(
System.Windows.Threading.DispatcherPriority.SystemIdle,
TimeSpan.FromSeconds(1),
new Action(
delegate()
{
RunImage();
}
));
}
));
thread.Start();
new System.Threading.ThreadStart(
delegate()
{
this.Dispatcher.Invoke(
System.Windows.Threading.DispatcherPriority.SystemIdle,
TimeSpan.FromSeconds(1),
new Action(
delegate()
{
RunImage();
}
));
}
));
thread.Start();
#6
BeginInvoke和 Invoke方法所调用的委托都是在 UI 线程中执行的。
支持楼上的
支持楼上的