2014-12-05 61 views
0

我有一個Windows窗體應用程序在c#Visual Studio 2012與button_click上。我試圖做到以下幾點。 應用程序工作正常,每當我點擊我的按鈕應用程序做我想做的事情。問題是我需要每天都這樣做,而且我不會去那裏手動點擊我自己的按鈕。有一種方法可以自動將時間安排自動啓動按鈕每天?所以我會一直打開應用程序,所以每天都會在某個時間按下按鈕。button1_Click Windows窗體c#創建調度程序任務

非常感謝。

+4

創建由Windows調度引發的控制檯應用程序,或創建一個Windows服務它使用Quartz來執行一項工作。 – 2014-12-05 15:51:22

回答

-2

使用間隔爲1000的計時器並檢查當前日期是否已更改。比做「按鈕點擊」。

+1

這是非常脆弱的,對於像這樣長時間運行的東西,這不是一個很好的方法。這意味着代碼需要24/7全天候運行,而不是隻有當它有工作要做時(意味着在任何時候消耗系統資源),重新啓動計算機會混淆它,用戶可能很容易不經意地關閉它等等這並不是所有的日期時間問題。例如,當有人改變系統時鐘時會發生什麼。 – Servy 2014-12-05 15:55:34

+0

這通常不是你如何做到的。 – 2014-12-05 15:56:06

-1

你需要做的是將你的事件處理程序代碼集中到類庫中。然後您的事件處理程序可以調用該集中式方法。完成之後,您可以創建一個控制檯應用程序或Windows服務,如上面提到的L-Three。

// Event handler 
private void button1_Click(object sender, EventArgs args) 
{ 
     LibraryClass.DoStuff(...); // Pass whatever parameters might be needed 
} 

// In a separate project 
public class LibraryClass 
{ 
     public static void DoStuff(...) 
     { 
      // Move the code from your event handler here 
     } 
} 

// In a console app 
class Program 
{ 
     static void Main(string [] args) 
     { 
      LibraryClass.DoStuff(...); // pass whatever parameters are needed 
     } 
} 
+0

嗨,感謝您的回答,這意味着這不能用Windows窗體來完成?它只會與控制檯應用程序一起工作?問題是我的應用程序是一個Web服務客戶端,我正在使用Web服務。謝謝 – 2014-12-05 16:03:52

+0

這不是您唯一可以做到的方法,但我會強烈建議您使用任何類型的kludge,這會涉及單擊基於某種計時器的表單上的按鈕。沒有什麼能夠阻止您從類庫中使用Web服務。如果您將代碼集中到類庫中,則可以在控制檯應用程序或Winform中重新使用它。 – YtramX 2014-12-05 16:06:02

+0

@BenjaminArancibia爲什麼這是一個Web服務客戶端,這意味着您需要使用Winform應用程序而不是控制檯應用程序或Windows服務應用程序? – Servy 2014-12-05 16:08:29

-2

你好本傑明·阿蘭西維亞,你可以做到這一點,如下所示:

在其App.config中添加一個變量來保存任務時會發生的控制。

<appSettings>  
    <add key="executeDailyService" value="true"/> 
    <add key="dailyServiceTime" value="08:00-18:00"/> 
    <add key="dailyServiceInterval" value="1"/> 
</appSettings> 

executeDailyService =選項指示任務是否每天都在發生 dailyServiceTime =選項來指定,它可以發生在這裏可以自定義時間段例如:13:00-14:00 dailyServiceInterval =選項通知任務將發生的天數範圍如: 1 =每天2 =隔日... N

當你啓動應用程序啓動以下方法:

private void startTimer() 
{ 
    // Loads the variables that will be used 
    var executeDailyService = ConfigurationManager.AppSettings["executeDailyService"]; 
    var dailyServiceTime = ConfigurationManager.AppSettings["dailyServiceTime"]; 
    var dailyServiceInterval = ConfigurationManager.AppSettings["dailyServiceInterval"]; 
    // Validates that the variables are loaded 
    if (executeDailyService != "true" || String.IsNullOrEmpty(dailyServiceTime) || String.IsNullOrEmpty(dailyServiceInterval)) 
     return; 

    // Timer that controls the automatic execution of code 
    var timer = new Timer(); 
    timer.Interval = Convert.ToInt32(dailyServiceInterval) * 60 * 1000; // Calculates how often will run your code 
    timer.Elapsed += new ElapsedEventHandler(services); 
    timer.Start(); 
} 

private void services(object sender, ElapsedEventArgs e) 
{ 
    // Loads of the variable time period 
    var dailyServiceTime = ConfigurationManager.AppSettings["dailyServiceTime"]; 
    // Validates the contents of the variable is in the correct format 
    if (String.IsNullOrEmpty(dailyServiceTime) || !dailyServiceTime.IsMatch("^\\d{2}:\\d{2}-\\d{2}:\\d{2}$")) 
     return; 

    // Calculates whether the current time is within the range you set 
    var now = Util.Now(); 
    var dateBegin = Util.DateTime(now.Year, now.Month, now.Day, Convert.ToInt32(dailyServiceTime.Substring(0, 2)), Convert.ToInt32(dailyServiceTime.Substring(3, 2)), 0); 
    var dateEnd = Util.DateTime(now.Year, now.Month, now.Day, Convert.ToInt32(dailyServiceTime.Substring(6, 2)), Convert.ToInt32(dailyServiceTime.Substring(9, 2)), 0); 
    if (dateBegin > dateEnd || now < dateBegin || now > dateEnd) 
     return; 

    var timer = sender as Timer; 
    timer.Stop(); 

    // You code here 

    timer.Start(); 
} 
+1

這是*非常*脆弱,並不是一個很好的方式來自動化像這樣的東西運行這麼長一段時間的東西。這意味着代碼需要24/7全天候運行,而不是隻有當它有工作要做時(意味着在任何時候消耗系統資源),重新啓動計算機會混淆它,用戶可能很容易不經意地關閉它等等這並不是所有的日期時間問題。例如,當有人改變系統時鐘時會發生什麼。 – Servy 2014-12-05 16:17:45

+0

@Servy完全不同意你,你已經使用了Timer功能? This line: timer.Interval = Convert.ToInt32(dailyServiceInterval)* 60 * 1000; 只會讓你的代碼在運行時運行,他不會全天候運行。 .Net框架自己擁有這個控件。 但是另一種方法是創建一個控制檯程序並將代碼放在main方法中。然後在Windows任務計劃程序中添加該程序。 – picossi 2014-12-05 16:32:36

+0

這個過程將24/7活着。它不會一直積極地消耗CPU,但它的內存佔用將始終處於活動狀態,從而使內存泄漏的可能性非常真實,並導致我所解決的其他問題;用戶可以很容易地意外關閉它,重新啓動會攪亂它等等。這些都是實際過程全天候運行的事實的直接影響,而不是由操作系統通過諸如任務調度程序或作爲服務。 「定時器」主要用於較短的時間間隔。 – Servy 2014-12-05 16:35:07

相關問題