我想检查我的功能在x秒内执行了多少次,就像3秒我看到1个堆栈示例相似但不完整填充我的概率..
实际上我正在使用AUTOMATION UI并且我的事件已经多次执行了所以我有一个解决方案,我将对话的名称传递给我的函数并且需要检查天气,当在接下来的3中执行相同的功能时,相同的名称传递给函数-4秒是相同的,如果是的话我会返回我的事件处理程序,所以这是我的代码事件自动化ui
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;
}
答案 0 :(得分:0)
将名称和调用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 );