C# 长时间运行任务:在调用后报告进度或控制执行

C# 长时间运行任务:在调用后报告进度或控制执行,c#,callback,C#,Callback,我写了一个函数,它使用DLL作为它的用途。此函数所在的类具有用于监视此函数状态的dll回调以及我必须在另一个类中使用的字段。 当我在目标类中调用此函数时,我必须连续获取此字段值(用于进度条)或在此回调中传递“Halt”,但只有在函数完成执行/ 我怎么能这样做? 这是我的密码。我需要通过progressbar对话框取消引擎取消 class LibWrap //containing the LONG process with dll { public bool cancelThisBoringPr

我写了一个函数,它使用DLL作为它的用途。此函数所在的类具有用于监视此函数状态的dll回调以及我必须在另一个类中使用的字段。 当我在目标类中调用此函数时,我必须连续获取此字段值(用于进度条)或在此回调中传递“Halt”,但只有在函数完成执行/ 我怎么能这样做? 这是我的密码。我需要通过progressbar对话框取消引擎取消

 class LibWrap //containing the LONG process with dll
{
public bool cancelThisBoringProcess;
public int currentPecentage;
public delegate void  pfnCallback(int progress, out bool cancel);
public void showProgress(int progress, out bool cancel)
{
 cancel = cancelThisBoringProcess; 
 currentPecentage = progress;
} 
[DllImport("Lib.DLL", CallingConvention = CallingConvention.Cdecl)]
 public unsafe static extern byte* bufferOp(byte* data,pfnCallback del);
public unsafe  BitmapFrame engine(BitmapFrame incomingFrame)
{
//...
 fixed (byte* inBuf = incoming)
 {
var callback = new pfnCallback(showProgress);
byte* outBuf = bufferOp(inBuf, callback);//this is DLL function with callback to get out percentage //and pass cancel
      GC.KeepAlive(callback);
//....
}
}
}
class Main
{
void OnClick(object sender, RoutedEventArgs e)
{
ProgressDialog dlg = new ProgressDialog("");
LibWrap lwrap = new LibWrap();
DoWorkEventHandler handler = delegate { BitmapFrame bf = lwrap.engine(img)); };

            dlg.AutoIncrementInterval = 100;
            dlg.IsCancellingEnabled = true;
            dlg.Owner = Application.Current.MainWindow;
            dlg.RunWorkerThread(handler);
}
}

//ProgressDialog来自

我想您提到过,但这里是回调模式:

void LongOperation(object someParam, Function<int, bool> callback)
{
      int progress = 0;
      while (progress++<100)
      {
          // lengthy operation:
          Thread.CurrentThread.Sleep(1);

          if (!callback(progress)) 
              break;
      } 
}
void long操作(对象someParam,函数回调)
{
int progress=0;

虽然(progress++)我用代码更新了我的问题,我只是更新了我的答案以匹配您更新的问题。很抱歉再次提出问题。但是如何在30秒后停止进程,而不是按取消(例如)。如果我总是在30秒(bool stop)写入扫描,那么我需要两个不同的传入参数-一个为false,另一个为true(true=当用户选择此取消时停止)长操作(新[]{“some”,“data”},alwaysCancelAt30Seconds);-alwaysCancelAt30Seconds函数的参数没有当前状态,因为alwaysCancelAt30Seconds是任何委托,您可以只引用外部变量(例如,周围类的成员)对于state,我将这样做,但dll中的进程仍在运行。((我的进度窗口拍摄BackgroundWorker.CancelAsync();现在当我按canlel时,但无效处理中的循环(bool stop)仍在继续,直到结束。也许我的代码有点问题。((我试图像DoWorkEventHandler=delegate一样调用{BitmapFrame bf=lwrap.engine(数据,CancelByDemand);};其中int CancelByDemand(LibWrap libPtr){libPtr.SetCancelForProcess=stop;返回libPtr.GetCurrentPercentage;}LibWrap中的函数引擎描述为:公共不安全BitmapFrame引擎(数据dat,Func progressCallBack)
bool alwaysCancelAt30Seconds(int progress)
{
     if ((DateTime.Now - startTime).TotalSeconds <= 30)
     {
          form1.lblProgress.Text = progress.ToString();
     } else
     {
          form1.lblProgress.Text = "canceled due to timeout!";
          return false;     // means 'abort'
     }
     return true;           // means 'continue'
}

  // call site:
  LongOperation(new [] { "some", "data" }, alwaysCancelAt30Seconds);