2016-07-25 52 views
1

我要檢查多少時間我的函數在x秒樣3秒,我所看到的是相似但不完全填滿我的概率1個堆棧執行。例如..檢查函數在x秒內執行多少次? C#

其實我自動化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

一個計時器在一個單獨的線程中運行,它會不斷打印一個由您的函數修改的變量呢?讓計時器每隔x秒觸發一次報告。 – Takarii

+0

@Sinatr這不是一個方法的執行時間。 –

+0

@Sinatr這個問題是關於:如何檢查一個方法是否在最後n秒被調用 –

回答

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); 
+0

時間以毫升你的意思是1000 = 1秒或應該檢查對1,2 –

+0

我通過5000重複,但總是給出錯誤 –

+0

SOLUTIONS可接受一點修改或檢查請 –