C# 逐步执行

发布于 2024-09-06 03:25:03 字数 1022 浏览 7 评论 0原文

我正在构建一个使用扫描仪 API 和图像到其他格式转换器的应用程序。我有一个方法(实际上是一个单击事件)可以执行此操作:

  private void ButtonScanAndParse_Click(object sender, EventArgs e)
  {
     short scan_result = scanner_api.Scan();

     if (scan_result == 1)
        parse_api.Parse(); // This will check for a saved image the scanner_api stores on disk, and then convert it.
  }

问题是 if 条件 (scan_result == 1) 是立即评估的,所以它不起作用。

如何强制 CLR 等待 API 返回方便的结果。

注意

只需执行以下操作:

  private void ButtonScanAndParse_Click(object sender, EventArgs e)
  {
     short scan_result = scanner_api.Scan();
     MessageBox.Show("Result = " + scan_result);
     if (scan_result == 1)
        parse_api.Parse(); // This will check for a saved image the scanner_api stores on disk, and then convert it.
  }

它就会工作并显示结果。

有没有办法做到这一点,如何做?

非常感谢!

更新:

扫描仪 API 上有一个事件:

Public Event EndScan() // Occurs when the scanned the image.

但我不知道如何使用它。有什么想法吗?

I'm building an app that uses and scanner API and a image to other format converter. I have a method (actually a click event) that do this:

  private void ButtonScanAndParse_Click(object sender, EventArgs e)
  {
     short scan_result = scanner_api.Scan();

     if (scan_result == 1)
        parse_api.Parse(); // This will check for a saved image the scanner_api stores on disk, and then convert it.
  }

The problem is that the if condition (scan_result == 1) is evaluated inmediatly, so it just don't work.

How can I force the CLR to wait until the API return the convenient result.

NOTE

Just by doing something like:

  private void ButtonScanAndParse_Click(object sender, EventArgs e)
  {
     short scan_result = scanner_api.Scan();
     MessageBox.Show("Result = " + scan_result);
     if (scan_result == 1)
        parse_api.Parse(); // This will check for a saved image the scanner_api stores on disk, and then convert it.
  }

It works and display the results.

Is there a way to do this, how?

Thank you very much!

UPDATE:

Theres an event on the scanner API:

Public Event EndScan() // Occurs when the scanned the image.

But I don't know how to use it. Any Idea?

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

时间海 2024-09-13 03:25:03

这实际上取决于 API 的工作方式。如果scanner_api.Scan()被阻塞,那么它将位于该行等待结果。一旦得到结果,if 就会进行评估。这可能会导致您的 UI 变得无响应,因此您通常必须实现某种线程才能在后台执行此操作。我从你的问题猜测这不是这个 API 的工作方式。

另一种可行的方法是使用轮询。您经常检查以查看结果是什么。您不想不断地检查并耗尽所有资源(例如 CPU),因此您每隔一定时间间隔进行检查。谢尔顿用计时器的回答实现了这一点。

至少还有一种可行的方法是使用回调。您向 API 发送一个回调函数,以便在状态更新时调用。这可以作为您绑定的事件(委托)或作为参数传递的常规委托来实现。您经常会看到这些实现为“OnStatusChanged”、“OnCompleted”等。

基本上,这取决于 API 支持的内容。轮询通常有效,其他的必须得到支持。如果可能,请检查您的 API 文档以获取示例。

That really depends on how the API works. If scanner_api.Scan() is blocking, then it will sit at that line waiting for a result. Once it gets the result, the if will evaluate. This can cause your UI to become unresponsive, so you often have to implement some sort of threading to do it in the background. I'm guessing from your question that isn't the way this API works.

Another way this could work is with polling. You check every so often to see what the result is. You don't want to check constantly and use up all your resources (such as CPU), so you check at an interval. Sheldon's answer with a Timer achieves this.

At least one more way this may work is with a callback. You send the API a callback function to call when the status has updated. This can be implemented as events (delegate) you tie into or a regular delegate you pass as a parameter. You'll often see these implemented as "OnStatusChanged", "OnCompleted", etc.

Basically, it's down to what the API supports. Polling usually works, the others have to be supported. Check your API documentation for examples if possible.

失去的东西太少 2024-09-13 03:25:03

您可以使用计时器(请参阅MSDN:计时器类) 定期检查扫描是否已完成。

您也可以使用异步调用,在扫描过程完成时进行回调。请注意,这是更复杂的方法。

You can use a timer (see MSDN: Timer class) that periodically checks whether the scan already completed or not.

You can alternatively use an asynchronous call that calls back when the scanning process is finished. Note that this is the more complicated way.

孤独陪着我 2024-09-13 03:25:03

一种方法是使用计时器。设置计时器每隔几秒检查一次以检查 scan_result 中的值(需要将其提升为类级变量才能正常工作)。

所以,类似于:(

public class Scanning
{
    private System.Timers.Timer aTimer;
    short scan_result;

    public Scanning()
    {
        aTimer = new System.Timers.Timer(1000);

        // Hook up the Elapsed event for the timer.
        aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
    }

    private void ButtonScanAndParse_Click(object sender, EventArgs e)
    {
       aTimer.Enabled = true;

       scan_result = scanner_api.Scan();
    }    

    private void OnTimedEvent(object source, ElapsedEventArgs e)
    {
       if (scan_result == 1)
       {
          aTimer.Enabled = false;

          parse_api.Parse(); // This will check for a saved image the scanner_api stores on disk, and then convert it.
       }
    }
}

当然,这是未经测试的。YMMV。)

One way would be with a timer. Set the timer to check every few seconds to check the value in scan_result (which would need to be promoted to a class-level variable for this to work).

So, something like:

public class Scanning
{
    private System.Timers.Timer aTimer;
    short scan_result;

    public Scanning()
    {
        aTimer = new System.Timers.Timer(1000);

        // Hook up the Elapsed event for the timer.
        aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
    }

    private void ButtonScanAndParse_Click(object sender, EventArgs e)
    {
       aTimer.Enabled = true;

       scan_result = scanner_api.Scan();
    }    

    private void OnTimedEvent(object source, ElapsedEventArgs e)
    {
       if (scan_result == 1)
       {
          aTimer.Enabled = false;

          parse_api.Parse(); // This will check for a saved image the scanner_api stores on disk, and then convert it.
       }
    }
}

(This is untested, of course. YMMV.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文