如何将对象变量发送到通过 BackgroundWorker 实例调用 dowork 的方法中?

How to send a object variable into method which calls dowork by BackgroundWorker instance?

我在使用 BackgroundWorker class 时看到了 createobjref 属性。我正在尝试将一个对象变量发送到调用 dowork 的方法中。因此,我将找出哪个 BackgroundWorker 实例调用方法。

我在下面留下了代码片段供您更好地学习

            bw_daily = new BackgroundWorker();
            bw_daily.WorkerReportsProgress = true;
            bw_daily.WorkerSupportsCancellation = true;
            bw_daily.DoWork += bw_daily_DoWork;
            bw_daily.RunWorkerCompleted += bw_daily_WorkerCompleted;
    
            bw_daily_2 = new BackgroundWorker();
            bw_daily_2.WorkerReportsProgress = true;
            bw_daily_2.WorkerSupportsCancellation = true;
            bw_daily_2.DoWork += bw_daily_DoWork;
            bw_daily_2.RunWorkerCompleted += bw_daily_WorkerCompleted;

调用方法;​​

protected void bw_daily_DoWork(object sender, DoWorkEventArgs e)

我认为 createobjref 可能对解决我的问题有用,但我没有在 Microsoft 文档或任何地方找到详细信息。

最后,如果你有更好的解决方案,请与我分享。

我已经这样解决了我的问题;

我创建了一个 class,它继承了 BackGroundWorker 并添加了一个 bool 属性。

    class CustomBackGroundWorker : BackgroundWorker
    {
        public DateTime RunDate
        {
            get;
            set;
        }

        public TimeSpan Period
        {
            get;
            set;
        }

        public Exception Exception
        {
            get;
            set;
        }

        public bool IsOrganization
        {
            get;
            set;
        }
    }

然后我刚刚调用并使用了;

CustomBackGroundWorker bw_daily = new CustomBackGroundWorker();
bw_daily.WorkerReportsProgress = true;
bw_daily.WorkerSupportsCancellation = true;
bw_daily.DoWork += bw_daily_DoWork;
bw_daily.RunWorkerCompleted += bw_daily_WorkerCompleted;
bw_daily.IsOrganization = true;