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();
}
这里的负载测试是...一种从数据库中选取少量图像并显示它们的方法。如果在页面初始化时运行该方法,该方法工作正常,但是当我在后台工作线程中像这样加载它们时它没有给出任何输出.....这是方法负载测试。
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
{
}
}
我正在使用后台工作线程来显示加载进度条的弹出窗口,直到页面加载,弹出窗口是一个功能,将显示一个新的弹出窗口,上面写着正在加载.... 负载测试方法在任何地方都可以正常工作,但它不适用于后台工作者......功能负载测试从数据库中选择一些图像,这需要时间,我显示一个弹出窗口,直到图像被加载并在option_2.source,option_2.source,option_3.source和option_4.source中分发......
不要直接从后台线程更改控件属性。而是将它们发送到 UI 线程以使用它们。执行此操作的一种方法是通过BackgroundWorker.ReportProgress Method (Int32, Object)
另请参阅在 WPF 中使用后台工作线程更新 UI
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;
}
编辑:初始化后增加了对镜像的Freeze()
调用。这是必要的,以便允许访问创建映像的工作线程之外的映像。
作为旁注:请用一些实际的错误检查替换您的空catch { }
块......我想您只是在那里抑制了有助于识别问题的相关例外。
后台工作者具有 ReportProgress 方法,您可以使用该方法向 UI 报告后台的定期进度。
将工作人员报告进度设置为 true,因为这默认为 false。
bw.WorkerReportsProgress = true;
然后将进度报告事件挂接到 UI 更新方法(bw_ProgressChanged(,你已经在这样做了。
bw.ProgressChanged += bw_ProgressChanged;
然后在要更新 Do_Work 方法中的 UI 的任何位置调用 ReportProgress 方法。从发送方对象获取工作线程并传递到 loadtest 方法并调用,如下所示。
bw.ReportProgress(progressPercentage); // This would be % completed you want to display.
这应该有效。