C# wpf后台工作程序未显示来自该方法的数据
这里的负载测试是…一种从数据库中提取一些图像并显示它们的方法。如果在页面初始化时运行该方法,则该方法可以正常工作,但当我在后台工作程序中像这样加载它们时,该方法不会给出任何输出……下面是方法loadtestC# wpf后台工作程序未显示来自该方法的数据,c#,wpf,C#,Wpf,这里的负载测试是…一种从数据库中提取一些图像并显示它们的方法。如果在页面初始化时运行该方法,则该方法可以正常工作,但当我在后台工作程序中像这样加载它们时,该方法不会给出任何输出……下面是方法loadtest private void bw_DoWork(object sender, DoWorkEventArgs e) { loadtest(); } private void bw_RunWorkerCompleted(object sender, Ru
private void bw_DoWork(object sender, DoWorkEventArgs e)
{
loadtest();
}
private void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
//Progress Bar Window close
pop.Close();
}
private void bw_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
pop.prgTest.Value = e.ProgressPercentage;
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
//Background Worker code///
bw.WorkerReportsProgress = true;
bw.DoWork += bw_DoWork;
bw.ProgressChanged += bw_ProgressChanged;
bw.RunWorkerCompleted += bw_RunWorkerCompleted;
bw.RunWorkerAsync();
//Progress Bar Window
pop.Show();
}
我正在使用后台工作程序显示加载进度条的弹出窗口,直到页面加载,弹出窗口是一个功能,将显示一个新的弹出窗口,显示加载。。。。
loadtest方法在任何地方都能正常工作,但它不能与backgroundworker一起工作…Function loadtest从数据库中拾取一些图像,这需要时间,我将显示一个弹出窗口,直到图像被加载并在选项_2.source、选项_2.source中显示,选项3.source和选项4.source………backgroundworker具有ReportProgress方法,您可以使用该方法向UI报告后台的定期进度 将WorkerReportsProgress设置为true,因为它默认为false
public void loadtest()
{
string query = "select*from question where id='" + 1 + "'";
MySqlConnection conDataBase = new MySqlConnection(constring);
MySqlCommand cmdDataBase = new MySqlCommand(query, conDataBase);
MySqlDataReader myReader;
try
{
conDataBase.Open();
myReader = cmdDataBase.ExecuteReader();
while (myReader.Read())
{
string qid = myReader.GetInt32("id").ToString();
byte[] imgg1q1 = (byte[])(myReader["question"]);
byte[] imgg2q1 = (byte[])(myReader["opt1"]);
byte[] imgg3q1 = (byte[])(myReader["opt2"]);
byte[] imgg4q1 = (byte[])(myReader["opt3"]);
byte[] imgg5q1 = (byte[])(myReader["opt4"]);
MemoryStream mstreamq1 = new MemoryStream(imgg1q1);
MemoryStream mstream1q1 = new MemoryStream(imgg2q1);
MemoryStream mstream2q1 = new MemoryStream(imgg3q1);
MemoryStream mstream3q1 = new MemoryStream(imgg4q1);
MemoryStream mstream4q1 = new MemoryStream(imgg5q1);
q1.BeginInit();
q1.StreamSource = mstreamq1;
q1.CacheOption = BitmapCacheOption.OnLoad;
q1.EndInit();
// Assign the Source property of your image
q_image.Source = q1;
q1opt1.BeginInit();
q1opt1.StreamSource = mstream1q1;
q1opt1.CacheOption = BitmapCacheOption.OnLoad;
q1opt1.EndInit();
option_1.Source = q1opt1;
q1opt2.BeginInit();
q1opt2.StreamSource = mstream2q1;
q1opt2.CacheOption = BitmapCacheOption.OnLoad;
q1opt2.EndInit();
option_2.Source = q1opt2;
q1opt3.BeginInit();
q1opt3.StreamSource = mstream3q1;
q1opt3.CacheOption = BitmapCacheOption.OnLoad;
q1opt3.EndInit();
option_3.Source = q1opt3;
q1opt4.BeginInit();
q1opt4.StreamSource = mstream4q1;
q1opt4.CacheOption = BitmapCacheOption.OnLoad;
q1opt4.EndInit();
option_4.Source = q1opt4;
}
conDataBase.Close();
}
catch
{
}
}
然后将进度报告事件连接到您的UI更新方法bw_ProgressChanged,您已经在这样做了
bw.WorkerReportsProgress = true;
然后在Do_Work方法中,在任何需要更新UI的地方调用ReportProgress方法。从发送方对象获取worker并传递到loadtest方法中,然后像下面那样调用
bw.ProgressChanged += bw_ProgressChanged;
这应该可以工作。不要直接从后台线程更改控件属性。而是将它们发送到UI线程以使用它们。实现这一点的一种方法是通过see 及
公共无效负载测试(BackgroundWorker bw)
{
//…您的代码
q1.BeginInit();
q1.StreamSource=mstreamq1;
q1.CacheOption=BitmapCacheOption.OnLoad;
q1.EndInit();
//通过冻结图像,UI线程将可以使用它
q1.冻结();
//不要直接指定图像的源属性
//q_image.Source=q1;
//而是向UI线程报告进度:
ReportProgress.ReportProgress(25,新元组(q_图像,q1));
//…您的代码
}
私有void bw_ProgressChanged(对象发送方,ProgressChangedEventArgs e)
{
pop.prgTest.Value=e.ProgressPercentage;
//在UI线程上分配图像源
var data=e.UserState作为元组;
data.Item1.Source=data.Item2;
}
Edit:在初始化后添加了对image的Freeze()
调用。为了允许访问创建映像的工作线程之外的映像,这是必需的
作为旁注:请将空的
catch{}
块替换为一些实际的错误检查。。。我想您只是抑制了有助于确定问题的相关例外情况。我已经做了所有这些,。。。。。。。。。。。这不是问题……我使用的是isdetminate进度条问题是,当我在后台工作程序中调用此方法时,loadtest()方法没有显示输出……否则,当我在没有backgroundworker的情况下使用此方法时,它工作得很好除了您的后台问题之外,请查看您的查询,我相信这只是一个样本。。。您的硬值为1,但如果来自外部源,请确保参数化查询,否则您将打开sql-injection。如果您刚刚启动项目,您可以考虑使用<代码>任务<代码> s,而不是<代码>后台工作人员<代码>,因为它们在WPF和MVVMI中更容易实现。不知道什么是问题代码,但没有显示图像。我不理解这个问题……我可以联系您的电子邮件吗?或者我需要帮助的ItStAcExpod是提供的。公开可用的问题和答案,因此没有电子邮件。不过,稍后我将尝试创建一个示例项目,以尽可能好地模拟您的代码。也许我能重现这个问题…项目很简单,它显示数据库中的图像,并显示一个加载图标,直到图像加载并存储在变量中,以便我以后也可以调用它们………我什么时候可以期望您的项目对您如此好………您将发送到哪里“我…?”索拉布编辑了我的回答。缺少对映像的Freeze()
调用,因此ProgressChanged
中的分配将正常工作。
bw.ReportProgress(progressPercentage); // This would be % completed you want to display.
private void bw_DoWork(object sender, DoWorkEventArgs e)
{
loadtest(sender as BackgroundWorker);
}
public void loadtest(BackgroundWorker bw)
{
//... your code
q1.BeginInit();
q1.StreamSource = mstreamq1;
q1.CacheOption = BitmapCacheOption.OnLoad;
q1.EndInit();
// by freezing the image, it will become available to the UI thread
q1.Freeze();
// Don't directly assign the Source property of your image
// q_image.Source = q1;
// Instead, report progress to the UI thread:
bw.ReportProgress.ReportProgress(25, new Tuple<Image, ImageSource>(q_image, q1));
//... your code
}
private void bw_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
pop.prgTest.Value = e.ProgressPercentage;
// assign the image source on the UI thread
var data = e.UserState as Tuple<Image, ImageSource>;
data.Item1.Source = data.Item2;
}