本文介绍了C#后台工作人员除了我自己以外,不会触发任何电脑上的dowork事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的一个应用程序中与后台工作人员有一个非常奇怪的问题。我最近修改了一些GUI修复程序,但现在当另一台PC上的某人运行.exe或安装我的OneClick部署时,后台工作人员不会进入dowork事件。两个版本之间的逻辑并没有改变。我已经做了一个比较,除了添加更多的错误记录没有任何改变。



我已经去了在dowork事件中包含消息框和断点,但它除了在我自己的电脑上,绝对不要进入它。即使我远程调试也没有进入DoWork事件



任何建议?



按钮点击甚至调用runworkerasync事件

  private void ScanButton_Click_1(object sender,RoutedEventArgs e)
{

扫描=新的BackgroundWorker();
Scan.WorkerReportsProgress = true;
Scan.DoWork + = new DoWorkEventHandler(Scan_DoWork);
Scan.ProgressChanged + = new ProgressChangedEventHandler(Scan_ProgressChanged);
Scan.RunWorkerCompleted + = new RunWorkerCompletedEventHandler(Scan_RunWorkerCompleted);
Scan.RunWorkerAsync();
}

以下是运行DoWork事件的代码。 Ive删除了为简单起见所做的其他功能。它所做的只是运行一个返回一个字符串并将其放在名为scanresults的列表中的函数。

  private BackgroundWorker扫描; 

public void Scan_DoWork(object sender,DoWorkEventArgs e)
{

System.Windows.Forms.MessageBox.Show(Inside the DoWork);

BackgroundWorker bw = sender作为BackgroundWorker;

float percentageDone = 0.0F;

ScanResults = new List< string>();

尝试
{

System.Windows.Forms.MessageBox.Show(做第一个扫描检查);

ScanResults.Add(Functions.LocalComputerName());
percentageDone =((1 / 1f)* 100f);
bw.ReportProgress((int)percentageDone);

}
catch(Exception ex)
{
System.Windows.MessageBox.Show(ex.Message,Error Got meets,MessageBoxButton.OK,MessageBoxImage。感叹);
}
}


public void Scan_RunWorkerCompleted(object sender,RunWorkerCompletedEventArgs e)
{
try
{
if(ScanResults.Count == 0)
{
System.Windows.Forms.MessageBox.Show(Empty);
return;
}
MachineNameBox.Text = ScanResults [0];
}
catch(Exception ex)
{
System.Windows.MessageBox.Show(ex.Message,Error GET Started,MessageBoxButton.OK,MessageBoxImage.Exclamation);
}
}

它完全忽略了dowork事件并输入了runworkercomplete事件并且传递索引的错误显然是因为列表没有被填充为已被跳过的函数。再次,这完全可以在我的电脑上工作,而不是在别人上,无论我如何将它发布给他们。



感谢任何帮助

解决方案

我的猜测是你的 DoWork 引发异常,所以你的 RunWorkerCompleted 被调用。



请注意,BGW的 DoWork 方法不会被抓到一个try ... catch在 RunWorkerCompleted ;而是,模式是检查 RunWorkerCompleted RunWorkerCompletedEventArgs中的错误 code>参数不为空。如果这不是null,你有一个例外。



您可以重新编写 RunWorkerCompleted 代码,如下所示: p>

  public void Scan_RunWorkerCompleted(object sender,RunWorkerCompletedEventArgs e){
if(e.Error!= null){
//你有异常,你可以通过e.Error属性来检查。
} else {
// DoWork中没有异常。
try {
if(ScanResults.Count == 0){
System.Windows.Forms.MessageBox.Show(Empty);
return;
}
MachineNameBox.Text = ScanResults [0];
} catch(Exception ex){
System.Windows.MessageBox.Show(ex.Message,Error Got meets,MessageBoxButton.OK,MessageBoxImage.Exclamation);
}
}
}

请参阅获取更多信息,比我的更好的例子。


I'm having a really weird issue with a background worker in one of my applications. I recently overhauled it slightly with some GUI fixes but now when someone on another PC runs the .exe or installs my OneClick deployment the background worker doesn't enter the dowork event. The logic has not changed between the 2 releases. I've done a compare and besides adding more error logging nothing has changed.

I've gone as far as to include message boxes and breakpoints in the dowork event but it never enters it anywhere besides on my own PC. Even when i remote debug it doesnt enter the DoWork event

Any suggestions?

Button click even calls the runworkerasync event

private void ScanButton_Click_1(object sender, RoutedEventArgs e)
    {

        Scan = new BackgroundWorker();
        Scan.WorkerReportsProgress = true;
        Scan.DoWork += new DoWorkEventHandler(Scan_DoWork);
        Scan.ProgressChanged += new ProgressChangedEventHandler(Scan_ProgressChanged);
        Scan.RunWorkerCompleted += new RunWorkerCompletedEventHandler(Scan_RunWorkerCompleted);
        Scan.RunWorkerAsync();
    }

Below is the code that runs the DoWork event. Ive removed the other functions it does for simplicity. All it does is run a function that returns a string and places it inside of the list called scanresults.

private BackgroundWorker Scan;

    public void Scan_DoWork(object sender, DoWorkEventArgs e)
    {

        System.Windows.Forms.MessageBox.Show("Inside the DoWork");

        BackgroundWorker bw = sender as BackgroundWorker;

        float percentageDone = 0.0F;

        ScanResults = new List<string>();

        try
        {

            System.Windows.Forms.MessageBox.Show("Doing first scan check");

            ScanResults.Add(Functions.LocalComputerName());
            percentageDone = ((1 / 1f) * 100f);
            bw.ReportProgress((int)percentageDone);

        }
        catch (Exception ex)
        {
            System.Windows.MessageBox.Show(ex.Message, "Error Encountered", MessageBoxButton.OK, MessageBoxImage.Exclamation);
        }
    }


public void Scan_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        try
        {
            if (ScanResults.Count == 0)
            {
                System.Windows.Forms.MessageBox.Show("Empty");
                return;
            }
            MachineNameBox.Text = ScanResults[0];
        }
        catch (Exception ex)
        {
            System.Windows.MessageBox.Show(ex.Message, "Error Encountered", MessageBoxButton.OK, MessageBoxImage.Exclamation);
        }
    }

It completely ignores the dowork event and enters the runworkercomplete event and passes a error for the index obviously because the list hasnt been filled as the function do that has been skipped. Again this works perfectly on my PC but not on others no matter how i release it to them.

Thanks for any assistance

解决方案

My guess is that your DoWork is throwing an exception and so your RunWorkerCompleted is called.

Note that exceptions thrown in a BGW's DoWork method will not be caught in a try...catch in RunWorkerCompleted; instead, the pattern is to check if the Error property in RunWorkerCompleted's RunWorkerCompletedEventArgs parameter is not null. If this is not null, you have an exception.

You might rework your RunWorkerCompleted code like this:

public void Scan_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) {
    if (e.Error != null) {
        // You have an exception, which you can examine through the e.Error property.
    } else {
        // No exception in DoWork.
        try {
            if (ScanResults.Count == 0) {
                System.Windows.Forms.MessageBox.Show("Empty");
                return;
            }
            MachineNameBox.Text = ScanResults[0];
        } catch (Exception ex) {
            System.Windows.MessageBox.Show(ex.Message, "Error Encountered", MessageBoxButton.OK, MessageBoxImage.Exclamation);
        }
    }
}

See BackgroundWorker.RunWorkerCompleted Event for more info and a better example than mine.

这篇关于C#后台工作人员除了我自己以外,不会触发任何电脑上的dowork事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 14:49