繁体   English   中英

检查函数在x秒内执行多少次? C#

[英]check how many time the function execute in x seconds? c#

我想检查我的函数在3秒钟之内在x秒内执行了多少次,我已经看过1个类似的堆栈示例,但没有完全填满我的概率。

实际上,我在AUTOMATION UI上工作,并且事件被多次执行,因此我有一个解决方案,即将对话的名称传递给我的函数,并且需要检查天气,在接下来的3-4秒钟执行相同的函数时,同一个名称会传递给函数如果是的话是相同的,我将返回事件处理程序,所以这是事件自动化用户界面的代码

Automation.AddAutomationEventHandler(
    WindowPattern.WindowOpenedEvent,
    AutomationElement.RootElement,
    System.Windows.Automation.TreeScope.Subtree,
    (sender, e) =>
    {
        string  dialogueName = sd.Current.Name;
        if (element.Current.LocalizedControlType == "Dialog")
        {
            starttimer(dialogueName );//how to get returned value there
        }
    }
});

功能码

public static nameRecent;
public bool checkerfunctionOFname(string name )
{
    if (nameRecent==name)
    {
        return;
    }
}

原因为什么我需要计时3-4秒是假设用户打开一个保存为对话,但关闭再经过10 EC再次打开,以便本场比赛之前的开放式名称是静态的,但是当他打开有史以来保存为对话然后重复相同它在3秒内具有相同的名称,因此如果函数再次执行是3秒,则返回false等

代码的解决方案,但在函数返回false时进行排列

     public static string  globalfucntiontime;
    public static string globalfucntionname;
    int _counter = 0;
    Timer timer;
  public void  starttimer(string name){

  _counter = 0;
  timer = new Timer();
  timer.Interval = 1000;
  timer.Tick += (sender, args) =>
        TimerEventProcessor(name);   //how to get its returned value
    globalfucntiontime = _counter.ToString();
    timer.Start();


 }

 private bool  TimerEventProcessor(string name)
   {
  globalfucntionname = name; 
  if (_counter <= 3 && name == globalfucntionname)
  {

      return false;
  }
  else if (name !=  globalfucntionname)
  {


  }
   globalfucntiontime = _counter.ToString();
   _counter += 1;

  return true;
}

将名称和调用的timstamp存储到字典中。 现在,您可以询问该词典,是否在最近n秒钟内调用了该名称。

public class Foo
{
    private readonly IDictionary<string,int> lastcalled = new Dictionary<string,int>();

    public void RegisterCallOf( string name )
    { 
        int now = Environment.TickCount;
        if ( lastcalled.ContainsKey( name ) )
            lastcalled[name] = now;
        else
            lastcalled.Add( name, now );
    }

    public bool WasCalledDuringLast( string name, int milliseconds )
    {
        int now = Environment.TickCount;
        if ( lastcalled.ContainsKey( name ) )
          return now - lastcalled[name] <= milliseconds;
        return false; 
    }
}

使用范例

// check if that "bar" was already called within the last 3000ms
if ( !foo.WasCalledDuringLast( "bar", 3000 ) )
{
    // it was not, so now we register this call
    foo.RegisterCallOf( "bar" );
    // and now call that bar method
}

更新资料

为了更容易使用,您可以使用

public void ExecuteIfWasNotCalledDuringLast( string name, int milliseconds, Action action )
{
    if ( !WasCalledDuringLast( name, milliseconds ) )
    {
        RegisterCallOf( name );
        action();
    }
}

然后您将使用该方法

foo.ExecuteIfWasNotCalledDuringLast( "bar", 3000, barAction );

暂无
暂无

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

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