检查函数以x秒为单位执行多少次?c#
我想检查我的函数在x秒内执行了多少次,比如3秒钟,我看到了一个堆栈示例,它与我的问题相似,但没有完全填满我的问题 实际上,我正在处理自动化UI,我的事件执行了很多次,所以我有一个解决方案,我将对话的名称传递给我的函数,并且需要检查在接下来的3-4秒内执行的相同函数是否相同,如果是,我将返回我的事件处理程序,因此这是我的事件自动化UI代码检查函数以x秒为单位执行多少次?c#,c#,C#,我想检查我的函数在x秒内执行了多少次,比如3秒钟,我看到了一个堆栈示例,它与我的问题相似,但没有完全填满我的问题 实际上,我正在处理自动化UI,我的事件执行了很多次,所以我有一个解决方案,我将对话的名称传递给我的函数,并且需要检查在接下来的3-4秒内执行的相同函数是否相同,如果是,我将返回我的事件处理程序,因此这是我的事件自动化UI代码 Automation.AddAutomationEventHandler( WindowPattern.WindowOpenedEvent, A
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秒后关闭,再次打开,因此这与之前打开的静态名称匹配,但当他打开另存为对话框时,相同的名称会在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;
}
公共静态字符串globalfunctiontime;
公共静态字符串globalFunctionName;
int _计数器=0;
定时器;
public void starttimer(字符串名称){
_计数器=0;
定时器=新定时器();
计时器。间隔=1000;
timer.Tick+=(发送方,参数)=>
TimerEventProcessor(名称);//如何获取其返回值
GLOBALFUNCTIONTIME=_counter.ToString();
timer.Start();
}
专用布尔TimerEventProcessor(字符串名称)
{
GLOBALFUNCTIONNAME=名称;
if(_counter将名字和调用的时间戳存储到一个字典中。现在你可以询问这个字典,如果这个名字是在最后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;
}
}
更新
为了便于使用,您可以使用
public void ExecuteIfWasNotCalledDuringLast( string name, int milliseconds, Action action )
{
if ( !WasCalledDuringLast( name, milliseconds ) )
{
RegisterCallOf( name );
action();
}
}
你将使用这种方法
foo.ExecuteIfWasNotCalledDuringLast( "bar", 3000, barAction );
将名称和调用标记存储到字典中。现在,您可以询问该字典,该名称是否在最后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;
}
}
更新
为了便于使用,您可以使用
public void ExecuteIfWasNotCalledDuringLast( string name, int milliseconds, Action action )
{
if ( !WasCalledDuringLast( name, milliseconds ) )
{
RegisterCallOf( name );
action();
}
}
你将使用这种方法
foo.ExecuteIfWasNotCalledDuringLast( "bar", 3000, barAction );
在一个单独的线程中运行一个计时器,它不断打印一个被函数修改的变量。让计时器每x秒触发一次报告。@Sinatr这不是一个方法的执行时间。@Sinatr这个问题是关于:如何检查一个方法在最近的n个线程中是否已经被调用seconds@SirRufo,同意。没有首先要解决这个问题。我会避免像这样推用户(打开对话而不涉及他)。如果用户已经在做某事(例如在某处编辑文本),然后你的对话框在打字的中间弹出,那么你可以向他展示打开对话框(例如按钮)的选项。。然后用户可以在需要时自由单击该按钮,您可以反复显示该按钮,而不会产生任何后果。否则(如果您坚持保持原样),您必须同步对该方法的访问(请参阅。Monitor.TryEnter/Monitor.Exit
和try/finally
)。如果计时器运行在一个单独的线程中,该线程不断打印一个被函数修改的变量,那么如何处理?让计时器每x秒触发一次报告。@Sinatr这不是关于方法的执行时间。@Sinatr这个问题是关于:如何检查在最近的n个线程中是否已经调用了一个方法seconds@SirRufo,同意。没有首先要解决这个问题。我避免像这样推用户(打开对话框,而不让他参与)。如果用户已经在做某事(例如在某处编辑文本),然后你的对话框在打字的中间弹出,那么你可以向他展示打开对话框(例如按钮)的选项。。然后用户可以在需要时自由单击该按钮,您可以反复显示该按钮,而不会产生任何后果。否则(如果您坚持保持原样),您必须同步对该方法的访问(请参阅。Monitor.TryEnter/Monitor.Exit
和try/finally
).时间单位:mili您的意思是1000=1秒,还是我应该检查1,2我通过5000次功能重复,但总是给出错误的解决方案可接受,但很少修改或检查。出现问题时,它必须输入值并给出间隔时间,如5000毫秒exists@zulqarnainkhalil请检查编辑…请不要大声喊叫(使用大写字母就像喊叫)时间(单位:mili)您的意思是1000=1秒,还是我应该检查1,2我正在通过5000次功能重复,但总是给出错误的解决方案可接受,但很少修改或检查。出现问题时,它必须输入值并给出间隔时间,如5000毫秒exists@zulqarnainkhalil请检查编辑…请不要大声喊叫(使用大写字母就像喊叫)