c#backgroundworker和partial类 [英] c# backgroundworker and partial class

查看:120
本文介绍了c#backgroundworker和partial类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我的代码如下:


$

b $ b

 使用System; 
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Threading;
使用System.Text.RegularExpressions;
using System.Runtime.InteropServices;
using GluthGUI.Classes.XMLprofile;
using System.Xml.Linq;
using System.ComponentModel;


命名空间解决方案
{
partial class SolGUI:Form
{

private void startButton_Click(object sender,EventArgs e )
{



backgroundWorker1 = new AbortableBackgroundWorker();

if(startButton.Text ==Start)
{

XMLParsing();


DisableTextFields();

backgroundWorker1.RunWorkerAsync();

startButton.Text =Stop;

}
else if(startButton.Text ==Stop)
{

if(backgroundWorker1.IsBusy == true)
{
backgroundWorker1.Abort(); // error Abort()is not declared?!?!
backgroundWorker1.Dispose();
}

startButton.Text =开始;
DisableTextFields();
}
}
}

这是一个部分类将终止后台工作:

 使用System; 
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;
using System.Threading;

命名空间解决方案
{
public class AbortableBackgroundWorker:BackgroundWorker
{
private Thread workerThread;

protected override void OnDoWork(DoWorkEventArgs e)
{
workerThread = Thread.CurrentThread;
try
{
base.OnDoWork(e);
}
catch(ThreadAbortException)
{
e.Cancel = true; //我们必须将Cancel属性设置为true!
Thread.ResetAbort(); // Prevents ThreadAbortException propagation
}
}


public void Abort()
{
if(workerThread!= null)
{
workerThread.Abort();
workerThread = null;
}
}
}
}



问题是来自部分类的Abort()方法在具有相同命名空间的其他类中不可见。

解决方案

问题是,正在使用 BackgroundWorker 的类型定义 backgroundWorker1 ,因此您无法访问在 AbortableBackgroundWorker class。



直接将 AbortableBackgroundWorker 或在表单中手动声明 AbortableBackgroundWorker

  SolGUI:Form 
{
AbortableBackgroundWorker backgroundWorker1 = new AbortableBackgroundWorker();

您还需要确保从按钮单击事件中删除此行代码:

  backgroundWorker1 = new AbortableBackgroundWorker(); 

因为这会导致每次单击时都设置一个新实例,原来的实例为了中止它。您也不应该在任何时候处理对象,因为您在重新启动该进程时想要重用它,所以删除:

  backgroundWorker1.Dispose(); 

您还需要挂接您正在使用的任何事件,例如 Dowork 。你应该在你的表单构造函数中这样做:

  backgroundWorker1.DoWork + = new System.ComponentModel.DoWorkEventHandler(this.backgroundWorker1_DoWork ); 






我不会进入任何细节关于你是否应该中止线程,其他人已评论,这是你的选择,如果你想跟随他们的建议和做一些研究。我只是简单回答了手头的问题。就个人而言,我会使用内置的取消方法。


I have a problem implementing code i got from stackowerflow its about killing a backgroundworker process.

My code is as follows:

using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Threading;
using System.Text.RegularExpressions;
using System.Runtime.InteropServices;
using GluthGUI.Classes.XMLprofile;
using System.Xml.Linq;
using System.ComponentModel;


namespace Solution
{
    partial class SolGUI : Form
    {

         private void startButton_Click(object sender, EventArgs e)
        {            



backgroundWorker1 = new AbortableBackgroundWorker();

            if (startButton.Text == "Start")
            {

                XMLParsing();


                DisableTextFields();

                backgroundWorker1.RunWorkerAsync();     

                startButton.Text = "Stop";

            }
            else if (startButton.Text == "Stop")
            {

                if (backgroundWorker1.IsBusy == true)
                {
                    backgroundWorker1.Abort();  //error Abort() is not declared?!?!
                    backgroundWorker1.Dispose();
                }

                startButton.Text = "Start";
                DisableTextFields();
            }
        }
    }

This is a partial class which would terminate backgroundworker:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;
using System.Threading;

namespace Solution
{
    public class AbortableBackgroundWorker : BackgroundWorker
    {
        private Thread workerThread;

        protected override void OnDoWork(DoWorkEventArgs e)
        {
            workerThread = Thread.CurrentThread;
            try
            {
                base.OnDoWork(e);
            }
            catch (ThreadAbortException)
            {
                e.Cancel = true; //We must set Cancel property to true!
                Thread.ResetAbort(); //Prevents ThreadAbortException propagation
            }
        }


        public void Abort()
        {
            if (workerThread != null)
            {
                workerThread.Abort();
                workerThread = null;
            }
        }
    }
}

My problem is that Abort() method from partial class is not visible in other classes with same namespace.

解决方案

The problem is that you are defining the backgroundWorker1 with the type of BackgroundWorker, so you do not have access to the custom methods defined in your AbortableBackgroundWorker class.

Either add a AbortableBackgroundWorker directly to your designer, or declare the AbortableBackgroundWorker manually within your form:

partial class SolGUI : Form
{
    AbortableBackgroundWorker backgroundWorker1 = new AbortableBackgroundWorker();

You also need to make sure you remove this line of code from your button click event:

backgroundWorker1 = new AbortableBackgroundWorker();

because that is causing a new instance to be set each time you click and you will never get access to the original instance in order to abort it. You should also not dispose the object at any point as you will want to reuse it when you start the process again, so remove:

backgroundWorker1.Dispose();

You will also need to hook up any events you are using, such as DoWork. You should do this in your forms constructor like so:

backgroundWorker1.DoWork += new System.ComponentModel.DoWorkEventHandler(this.backgroundWorker1_DoWork);


I am not going to go in to any detail about whether or not you should be aborting threads, others have commented and that is your choice if you want to follow their advice and do some research. I have simply answered the question at hand. Personally though, I would use the built-in cancellation methods.

这篇关于c#backgroundworker和partial类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆