动态时间限制的睡眠背景工人

发布于 2025-02-09 01:11:00 字数 2169 浏览 1 评论 0原文

我正在开发Windows表单应用程序。就我而言,我必须实施具有智能效果的进度栏,以进行耗时的API呼叫。

    private void View_btnSubmit_Click(Object sender, EventArgs e) {
        
      this.view.ProgressBarVisible = true;
    
      this.bgw = new BackgroundWorker();
      this.bgw.WorkerSupportsCancellation = true;
      this.bgw.WorkerReportsProgress = true;
      this.bgw.DoWork += new DoWorkEventHandler(Bgw_DoWork);
      this.bgw.ProgressChanged +=
          new ProgressChangedEventHandler(Bgw_ProgressChanged);
      this.bgw.RunWorkerCompleted +=
          new RunWorkerCompletedEventHandler(Bgw_RunWorkerCompleted);
      this.bgw.RunWorkerAsync();
    }
    
    private void Bgw_DoWork(object sender, DoWorkEventArgs e) {
        
      // runjob
      connection = new Connection(...);
      response = connection.Runjob(...);
    
      if (response != null) {
        if (response.ResponseStatus == ResponseStatus.Completed &&
            response.StatusCode == HttpStatusCode.Created) {
          string jobId = JObject
                             .Parse(response.Content) ["job_id"]
                             .ToString();
          string status = "new";
          int delayTime;
    
          // set initial delay time
          delayTime = count * 2;
    
          // check job status by jobid
          // new|queued|processing|completed|accounted|failed
    
          do {
            Thread.Sleep(delayTime);
            EfResponse = connection.CheckJobStatus(jobId);
            status = JObject
                         .Parse(EfResponse.Content) ["process_state"]
                         .ToString();
            delayTime = 3;
          } while (!(status == "accounted" || status == "failed"));
        }
   }

private void Bgw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    this.view.ProgressBarVisible = false;
    this.view.Close();
    this.bgw.CancelAsync();
    this.bgw = null;
}

最初,我必须在调用connection.checkjobstatus(jobid)之前设置延迟时间,这是一个dyanamic值,在第一个工作状态检查后,必须延迟每个方法调用的3秒。 thread.sleep()没有在这里做我想做的事情。由于此connection.checkjobstatus(jobid)方法正在使用后台工作者运行,因此我需要在这里睡觉的方法。

I'm developing a windows forms application. In my case I have to implement a progress bar with marquee effect for time consuming API call.

    private void View_btnSubmit_Click(Object sender, EventArgs e) {
        
      this.view.ProgressBarVisible = true;
    
      this.bgw = new BackgroundWorker();
      this.bgw.WorkerSupportsCancellation = true;
      this.bgw.WorkerReportsProgress = true;
      this.bgw.DoWork += new DoWorkEventHandler(Bgw_DoWork);
      this.bgw.ProgressChanged +=
          new ProgressChangedEventHandler(Bgw_ProgressChanged);
      this.bgw.RunWorkerCompleted +=
          new RunWorkerCompletedEventHandler(Bgw_RunWorkerCompleted);
      this.bgw.RunWorkerAsync();
    }
    
    private void Bgw_DoWork(object sender, DoWorkEventArgs e) {
        
      // runjob
      connection = new Connection(...);
      response = connection.Runjob(...);
    
      if (response != null) {
        if (response.ResponseStatus == ResponseStatus.Completed &&
            response.StatusCode == HttpStatusCode.Created) {
          string jobId = JObject
                             .Parse(response.Content) ["job_id"]
                             .ToString();
          string status = "new";
          int delayTime;
    
          // set initial delay time
          delayTime = count * 2;
    
          // check job status by jobid
          // new|queued|processing|completed|accounted|failed
    
          do {
            Thread.Sleep(delayTime);
            EfResponse = connection.CheckJobStatus(jobId);
            status = JObject
                         .Parse(EfResponse.Content) ["process_state"]
                         .ToString();
            delayTime = 3;
          } while (!(status == "accounted" || status == "failed"));
        }
   }

private void Bgw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    this.view.ProgressBarVisible = false;
    this.view.Close();
    this.bgw.CancelAsync();
    this.bgw = null;
}

Initially I have to set a delay time for before calling connection.CheckJobStatus(jobId), it's a dyanamic value and after 1st job status check then have to delay 3sec for each method call. Thread.Sleep() is not doing what I wanted here. Since this connection.CheckJobStatus(jobId) method is running with background worker, I need a way to sleep bgw here.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文