简体   繁体   English

监视流程或方法所花费的时间

[英]Monitor time elapsed for a process or method

In my Android application I use TTS and Voice Recognition. 在我的Android应用程序中,我使用TTS和语音识别。 Despite using all the available callbacks to understand the status they are in, I still get situations that I cannot handle. 尽管使用所有可用的回调来了解它们所处的状态,但仍然遇到无法处理的情况。 Examples: 例子:

1) TTS reports successful initialisation, but fails to speak (known issue for some custom TTS engines (mentioning no names)). 1)TTS报告初始化成功,但是无法讲话(某些自定义TTS引擎的已知问题(未提及名称))。

2) Network access is lost after initial successful connection, which can result in a huge amount of lag before a network error is returned. 2)初始成功连接后,网络访问将会丢失,这可能会导致大量延迟,直到返回网络错误。

There are other situations that I won't bore you with. 在其他情况下,我不会让您感到厌烦。

I've read and digested time monitoring threads such as this and this so I understand how to accurately determine the time between X&Y, but it's a monitoring method that I'm after, which I can react to when a set limit is reached. 我已阅读并消化时间监视线程比如这个这个 ,所以我知道如何准确地确定X和Y之间的时间,但它是一个监测方法是我后,我可以达到设定限制时作出反应。

Excuse my pseudo-code, but I hold my hands up, I've no idea where to begin on this.. 请原谅我的伪代码,但是我举起手来,不知道从哪里开始。

    public boolean methodSuccess = false;

    public void troublesomeMethod() {
    if(// everything completed and I got a usual result) {
    methodSuccess = true;
    } 

    public boolean timeMonitor(int maxDuration) {

    // use System.currentTimeMillis() here as start value

    // monitor time elapsed here in loop
    // monitor methodSuccess in loop
    // return false if methodSuccess is set to true whilst in loop

    // break if maxDuration reached and return true

    }

Again, excuse the above code, I simply can't think how to achieve this... 同样,请原谅上面的代码,我根本想不出如何实现此目标...

In addition, if I started the above with: 另外,如果我从以下内容开始:

    boolean monitorThis = timeMonitor(5000); // let me know when 5 seconds have passed
    startTroublesomeMethod(); // the one I want to monitor

Then it would be pointless if I had to 'wait' on the boolean response being returned before startTroublesomeMethod() actually began! 然后,如果我必须在startTroublesomeMethod()实际开始之前不得不“等待”返回的布尔响应,那将毫无意义! Or the troublesomeMethod began before I started monitoring it... 或者麻烦的方法在我开始监视它之前就开始了...

    if(monitorThis) {
    // react to lagging method by destroying TTS or Listener Object
    } else {
    // the troublesomeMethod behaved normally - do nothing
    }

I hope in my confusion I've managed to express what I'm trying to achieve. 希望我能在困惑中设法表达出自己想要达到的目标。

I thank you in advance for your help. 预先感谢您的帮助。

You can use a Handler to post a delayed check. 您可以使用Handler来发布延迟检查。 For example: 例如:

    Handler mHandler = new Handler();
    mHandler.postDelayed(new Runnable() {
        @Override public void run() {
            // You can set a flag or if you run your method 
            // on another thread, you can interrupt that thread. 
        }
    }, 5000);

Keep in mind that the Handler is set to the same thread it was created in. If the method you are trying to monitor is running in the same thread, you may have problems. 请记住,将Handler设置为与创建该线程的线程相同。如果要监视的方法在同一线程中运行,则可能会遇到问题。 Call your method in another thread and keep a reference to that thread to interrupt it if necessary. 在另一个线程中调用您的方法,并在需要时保留对该线程的引用以将其中断。

    Thread mThread = new Thread(new Runnable() {
        @Override public void run() {
            // Call your method here and keep a reference to this thread.
        }
    });
    mThread.run();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM