尝试设置我在后台运行BackgroundWorker
的设置,然后在完成每个步骤时在单独的模式对话框中更新其进度。当然,问题是尝试从BackgroundWorker
线程更新进度对话框会导致跨线程问题。
代码:(有问题的行包含用***
包围的注释)
public void StartProcess()
{
// dialog that shows a simple marquee progress bar and a message
ProgressDialog progress = new ProgressDialog("Starting operation...");
// create background worker thread
BackgroundWorker bw = new BackgroundWorker();
bw.WorkerSupportsCancellation = true;
bw.WorkerReportsProgress = true;
bw.DoWork += Bw_DoWork;
bw.RunWorkerCompleted += Bw_RunWorkerCompleted;
if(!bw.IsBusy)
{
bw.RunWorkerAsync();
progress.ShowDialog();
}
}
private void Bw_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker bw = sender as BackgroundWorker;
if(bw.CancellationPending)
{
e.Cancel = true;
}
else
{
// perform step 1
// *** this line generates cross-thread problems ***
progress.Message = "Performing step 2...";
// etc...
}
}
这是进度对话框的代码:
public partial class ProgressDialog : Form
{
public string Message
{
get
{
return lblMessage.Text;
}
set
{
if(lblMessage.InvokeRequired)
{
// *** not sure how to invoke this by passing the "value" parameter ***
// *** returns a TargetParameterCountException ***
// *** trying to pass "value" in results in "Method name expected" error ***
lblMessage.Invoke(new UpdateMessageDelegate(SetMessage));
}
}
}
private delegate void UpdateMessageDelegate(string s);
private void SetMessage(string s)
{
lblMessage.Text = s;
}
}
如何使用set
方法和隐式value
参数安全地更改标签的文本值?
答案 0 :(得分:3)
尝试以这种方式调用它:
lblMessage.Invoke((Action)(() => SetMessage(value)));
然后您还可以避免创建私有UpdateMessageDelegate
委托。
答案 1 :(得分:2)
找到了一种方法:
table tr:first-child {
font-weight: bold;
}
@media (min-width: 300px) {
table {
display: inline-block;
}
}