如何在c#中指定时间后取消后台工作程序或取消不响应后台工作程序.
我已经搜索了论坛,但没有问我的问题是否符合我的问题...
我使用BackgroundWorker来处理非常耗时的操作.完成此操作后,应将按钮设置为启用,以便用户可以执行其他操作.
我正在使用WPF并且我正在遵循MVVM模式,因此我无法直接访问此按钮.我必须在BackgroundWorker_RunWorkerCompleted事件处理程序中调用一个方法,该方法将表示按钮enabled-state的Property设置为true.
这一切都很好,除了一件事:按钮只在我点击进入窗口(或最大化窗口,...)后重新绘制.多数民众赞成非常烦人,并花了我一整天摆脱这种行为,但我找不到解决方案......
BackgroundWorker_RunWorkerCompleted事件处理程序如下所示:
void fileLoadBackgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) {
SetButtonToEnabled(); }
Run Code Online (Sandbox Code Playgroud)
有谁知道如何解决这个问题?
编辑:
该按钮绑定到一个命令:
<Button Name="btnLoadTargetFile" Command="{Binding Path=LoadTargetCommand}" .../>
Run Code Online (Sandbox Code Playgroud)
这个命令是史密斯在他的博客(http://msdn.microsoft.com/en-us/magazine/dd419663.aspx)中所说的RelayCommand ,看起来像这样:
public RelayCommand LoadTargetCommand
{
get
{
if (loadTargetCommand == null)
{
loadTargetCommand = new RelayCommand(param => this.OnRequestLoadFile(BusinessLogic.CustomTypes.TreeType.Target), param => this.CanLoadTarget);
}
return loadTargetCommand;
}
set { loadTargetCommand = value; }
}
Run Code Online (Sandbox Code Playgroud)
this.CanLoadTarget由SetButtonToEnabled()设置为true; 方法
编辑2:
以下代码'有效':
fileLoadBackgroundWorker.RunWorkerAsync(argumentList);
while(fileLoadBackgroundWorker.IsBusy)
System.Windows.Forms.Application.DoEvents();
SetButtonToEnabled();
Run Code Online (Sandbox Code Playgroud)
但这是一些非常危险和丑陋的代码......
场景:
我有一个窗口表单,其中包含DataGridWiew3个预定义列.我在函数外部声明了3个变量并分配到函数内部.我有一个枚举东西的函数,并将它放在3列中,逐行:
string VARIABLE1;
string VARIABLE2;
string VARIABLE3;
private void FunctionEnumerateStuff()
{
foreach (StuffObject STUFF in StuffCollection)
{
VARIABLE1 = STUFF.SubStuff1.ToString();
VARIABLE2 = STUFF.SubStuff2.ToString();
VARIABLE3 = STUFF.SubStuff3.ToString();
DatagridWiew1.Rows.Add(VALUE1, VALUE2, VALUE3);
}
}
Run Code Online (Sandbox Code Playgroud)
我想要做的是从一个BackGroundWorker进程执行这个功能,这样应用程序的GUI将是平滑和响应的.我已经阅读了背景工作者,但我遇到了麻烦,因为所有的例子似乎都是完全不同的情况,而且大多数情况都非常复杂.
一些有用的教育灵魂可以帮助我和其他人有一个非常基本的例子,说明如何以最简单的方式使其工作.谢谢.
我想在后台线程上运行一个操作.当它完成后,我想检查发生的任何错误,并将它们重新抛出我的原始线程.
我正在使用背景工作者.在RunWorkerCompleted事件处理程序中抛出异常会导致未处理的异常 - 如果事件处理程序在后台线程上运行,这是有意义的.如果我有一个winform控件,我可以调用Invoke或BeginInvoke,但我没有在这个对象中使用winform控件,尽管它是一个winform项目.
如何重新抛出backgroundworker中发生的异常?
private void bgw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (e.Error != null)
{
// I want to throw an exception here, without causing an unhandled exception and without being able to call Invoke or BeginInvoke on a WinForm control.
}
else if (e.Cancelled)
{
// Do something useful
}
else
{
if (e.Result != null)
{
// Do something with the result
}
}
}
Run Code Online (Sandbox Code Playgroud)
我原以为RunWorkerCompleted事件处理程序将在原始调用线程上运行.也许背景工作者不是我在这种情况下所需要的.
我的主线程(WPF应用程序)中有一个ListBox对象.为什么我无法在BackgroundWorker线程中访问它.据我所知,每个线程的堆栈是分开的,但堆很常见.不是在堆中创建ListBox对象.在那种情况下,为什么它无法访问.我尝试将ListBox引用作为参数传递,并尝试在BackgroundWorker中访问其内容.在线程之间共享对象的概念是否与C++不同?
在执行耗时的python脚本时,我会用后台工作者管理IU以显示进度条.
当我不需要事件时OutputDataReceived,我已成功使用后台工作程序,但我正在使用的脚本打印了一些进度值,如("10","80",..),所以我必须听取事件OutputDataReceived.
我得到这个错误:This operation has already had OperationCompleted called on it and further calls are illegal.在这一行progress.bw.ReportProgress(v);.
我试图使用2个后台工作器实例,一个执行而另一个监听,它没有给出任何错误,但它似乎没有调用事件'OutputDataReceived'所以我没有在进度条中看到任何进展.
在我使用的代码下面:
private void execute_script()
{
progress.bw.DoWork += new DoWorkEventHandler( //progress.bw is reference to the background worker instance
delegate(object o, DoWorkEventArgs args)
{
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo.FileName = "python.exe";
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.Arguments = @".\scripts\script1.py " + file_path + " " + txtscale.Text;
//proc.StartInfo.CreateNoWindow = true;
//proc.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
proc.StartInfo.RedirectStandardOutput = true;
//proc.EnableRaisingEvents = …Run Code Online (Sandbox Code Playgroud) 我有一个简单的表单,上面有2个progressbars和1个backgroundworker.我有2个循环(一个在另一个循环中),我想报告每个循环的进度一旦递增.这是我的代码:
private void buttonStart_Click(object sender, EventArgs e)
{
workerCustomers.RunWorkerAsync();
}
private void workerCustomers_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
progressBar1.Value = e.ProgressPercentage;
progressBar2.Value = (int)e.UserState;
}
private void workerCustomers_DoWork(object sender, DoWorkEventArgs e)
{
for (int customer = 0; customer < 50; customer++)
{
int customerPercentage = ++customer * 100 / 50;
workerCustomers.ReportProgress(customerPercentage, 0);
for (int location = 0; location < 500; location++)
{
int locationPercentage = ++location * 100 / 500;
workerCustomers.ReportProgress(customerPercentage, locationPercentage);
}
workerCustomers.ReportProgress(customerPercentage, 0);
}
}
Run Code Online (Sandbox Code Playgroud)
当程序运行时,progressbar1会更新得很好,但progressbar2永远不会移动.如果我通过调试器运行它,我可以看到progressbar2的值被更改,没有图形化的更改.有任何想法吗?
我System.ComponentModel.AsyncOperation.VerifyNotCompleted在stacktraceWindows手机市场的报告中有一个错误.我正在使用background worker后台线程工作.此错误windows phone 8比在Windows Phone 7中发生的更多.
请问有人可以解释这个错误吗?
backgroundworker windows-phone-7 windows-phone-7.1 windows-phone-8
我想创建一个按钮,可以阻止我的后台工作程序并结束它正在处理的所有进程.
这是我的示例backgroundworker代码:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Try
If BackgroundWorker1.IsBusy <> True Then
BackgroundWorker1.RunWorkerAsync()
End If
Catch ex As Exception
End Try
End Sub
Private Sub BackgroundWorker1_DoWork(sender As System.Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
Dim counter As Integer = 1
Do
'updated code with stop function----------------
BackgroundWorker1.WorkerSupportsCancellation = True
If BackgroundWorker1.CancellationPending Then
e.Cancel = True
ProgressBar1.Value = 0
Exit Do
End If
'updated code with stop function----------------
ListBox1.Items.Add(counter)
ProgressBar1.Value = ((counter - 1) / limit) …Run Code Online (Sandbox Code Playgroud) 我正在使用后台工作程序使用vb.net操作我的WebBrowser上的一些元素.
例如,后台工作人员RFIDReader将检查我是否在特定链接上.
ElseIf TerminalBrowser.Url.ToString.Contains(baseUrl + "box-office/ticket-verification") And rf_code <> "" Then
' Insert RF Code in "rf_code" hidden text field
Me.Invoke(Sub() Me.TerminalBrowser.Document.GetElementById("rf_code").SetAttribute("value", rf_code.ToLower))
End If
Run Code Online (Sandbox Code Playgroud)
如果我点击我的RFID卡,这里会发生什么.它将rf_code在我的浏览器中包含我的元素的相应值.
现在我想要发生的是,我想检查容器本身(synchronize-rfid)是否存在(因为它是一个弹出窗口).见图片以供参考.
这是我们的代码.
If Me.TerminalBrowser.Document.GetElementById("synchronize-rfid") IsNot Nothing Then
' Code here
end if
Run Code Online (Sandbox Code Playgroud)
参考:https://stackoverflow.com/a/2022120/1699388
问题是,BackgroundWorker并没有按照上面的代码与UI进行真正的交互.
我有什么方法可以确定是否使用后台工作符存在该元素?
我想我已经做到了这一点但它不起作用.
Dim synchronize_rfid = Me.Invoke(Sub() Me.TerminalBrowser.Document.GetElementById("synchronize-rfid"))
If synchronize_rfid IsNot Nothing Then
' Code here
end if
Run Code Online (Sandbox Code Playgroud)