English 中文(简体)
WPF和背景工人
原标题:WPF and background worker

我看着世界森林论坛的窗口,想做一些工作。

当这项工作完成时,我想再次做。

我的法典:

private void InitializeBackgroundWorker()
        {
            m_stopCamera = false;
            m_worker = new BackgroundWorker();
            m_worker.DoWork += new DoWorkEventHandler(worker_DoWork);
            m_worker.RunWorkerCompleted += new     RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);
            m_worker.WorkerSupportsCancellation = true;
        }

unsafe void worker_DoWork(object sender, DoWorkEventArgs e)
{
  // my work....
  e.Result = new MyResultClass(p1,p2..);
}

private void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (e.Cancelled)
                return;


            if (e.Error != null)
            {
                Logger.LogException(e.Error.ToString());
                Window_Closed(this, new EventArgs());
            }
            try
            {

                //UI work..
                DetectionResult result = e.Result as DetectionResult;
                if (result.Cancel == true)
                {
                    m_DetectedObjID = result.DetectionID;
                    // PlaySound();
                    audio_MediaEnded(this, new EventArgs());
                }
                else
                     ((BackgroundWorker)sender).RunWorkerAsync();

            }
            catch (Exception ex)
            {

                Logger.LogException(ex.ToString());
                Window_Closed(this,new EventArgs());
            }

        }

我知道,这部法典是一个好的理由,即read子,可以适当地加以 de击。

for example.. when the code is running and i click the close button i can see in the event the the worker is busy all the time.. resulting an infinity loop..

 private void Window_Closed(object sender, EventArgs e)
        {

                if (m_worker.IsBusy)
                    m_worker.CancelAsync();

                while(m_worker.IsBusy);
                base.Close();
            }

        }

什么是错的? 增 编

最佳回答

页: 1 这种方法中的“取消”法应着眼于:

if ((m_worker.CancellationPending == true))
{
   e.Cancel = true;
   return;
}

直到你收到<代码>。 填满的手稿和你的休息室将停下来。


如果您认为,应当制定<条码>。 你真的错了。 这里是来自背景工作者CancelAsync(以下简称)的编造法:

public void CancelAsync()
{
  if (!this.WorkerSupportsCancellation)
    throw new InvalidOperationException(SR.GetString("BackgroundWorker_WorkerDoesntSupportCancellation"));
  this.cancellationPending = true;
}

各位可以看到,这只是打上了<条码>待决/代码>。 因此,正如第一张刀上所示,你必须在<条码>上核对这一财产。

问题回答

明确路线:

while(m_worker.IsBusy);//may be go to infinite loop

如果你想在这种背景下做一些事情,你可以这样做:

public void DoEvents()
{
    Application.Current.Dispatcher.Invoke(DispatcherPriority.Background, new Action(() =>
    {
        code;
        DoEvents();
    }));
}

该守则将在万国邮联获释时执行,因此不影响你主线的运行。





相关问题
Bring window to foreground after Mutex fails

I was wondering if someone can tell me what would be the best way to bring my application to the foreground if a mutex was not able to be created for a new instance. E.g.: Application X is running ...

How to start WinForm app minimized to tray?

I ve successfully created an app that minimizes to the tray using a NotifyIcon. When the form is manually closed it is successfully hidden from the desktop, taskbar, and alt-tab. The problem occurs ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

Handle DataTable.DataRow cell change event

I have a DataTable that has several DataColumns and DataRow. Now i would like to handle an event when cell of this DataRow is changed. How to do this in c#?

Apparent Memory Leak in DataGridView

How do you force a DataGridView to release its reference to a bound DataSet? We have a rather large dataset being displayed in a DataGridView and noticed that resources were not being freed after the ...

ALT Key Shortcuts Hidden

I am using VS2008 and creating forms. By default, the underscore of the character in a textbox when using an ampersand is not shown when I run the application. ex. "&Goto Here" is not ...

WPF-XAML window in Winforms Application

I have a Winforms application coded in VS C# 2008 and want to insert a WPF window into the window pane of Winforms application. Could you explain me how this is done.

热门标签