gpt4 book ai didi

c# - 函数运行时用于显示图片的线程

转载 作者:行者123 更新时间:2023-12-03 10:27:12 25 4
gpt4 key购买 nike

我有一个功能,需要很长时间。

函数运行时,我想显示一些图片,但是此代码不起作用。为什么 ?

(我必须仅使用框架4 )

这是代码XAML

<Image Width="33" Stretch="Uniform" Source="{Binding ImagePath, UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Left" Grid.Column="0"/>

这是代码
public bool Cmd_TestExe()
{
Thread trd_A = new Thread(MyThreadTask_ShowImg);
Thread_Status = false;
trd_A.Start();

if (My_Slow_Function(sCon) == false) {
displayMessage(Status_Failure);}
}





public void Wait(int sleep)
{
dynamic dFrame = new DispatcherFrame();

ThreadPool.QueueUserWorkItem(state =>
{
Thread.Sleep(sleep);
dFrame.Continue = false;
});
Dispatcher.PushFrame(dFrame);
}





public void MyThreadTask_ShowImg()
{
while (Thread_Status == false) {
Application.Current.Dispatcher.Invoke(new Action(() =>
{
if (Second(Now) % (2) == 0) {
ImagePath = "/Images/exit.png";
Wait(250);
} else {
ImagePath = "/Images/excel.png";
Wait(250);
Console.WriteLine(Now);
}
}));
}
}

...这是属性(property)
private string _ImagePath { get; set; }
public string ImagePath {
get { return _ImagePath; }
set {
_ImagePath = value;
OnPropertyChanged("ImagePath");
}
}

最佳答案

首先,您必须在Cmd_TestExe方法中返回 bool 值。

其次,我从未使用过DispatcherFrame,但这是我会做的:

public partial class MainWindow : Window
{

Thread myLittleThread;
bool stop;
public string ImageSource
{
set
{
myLittleImage.Source = new BitmapImage(new Uri(value));
}
}
public MainWindow()
{
InitializeComponent();
InitThread();
}

private void InitThread()
{
myLittleThread = new Thread(DoWork);
stop = false;
Application.Current.Exit += MyLittleApplication_Exit;
myLittleThread.Start();
}


private void MyLittleApplication_Exit(object sender,EventArgs e)
{
stop = true;
}

private void DoWork(){
string newImageSource;
while (!stop)
{
if (DateTime.Now.Second % 2 == 0)
{
newImageSource = "SomeRandomFooImage.png";
}
else
{
newImageSource = "MyLittleRandomImage.png";
}

Application.Current.Dispatcher.Invoke(new Action(() =>
{
ImageSource = newImageSource;
}));

Thread.Sleep(250);
}
}

}

和XAML:
<Image Name="myLittleImage" ></Image>

关于c# - 函数运行时用于显示图片的线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34044185/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com