开发者

C# Step by Step Execution

开发者 https://www.devze.com 2023-01-03 16:17 出处:网络
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:

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?


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.


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.


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.)

0

精彩评论

暂无评论...
验证码 换一张
取 消