我一直在使用c#中的计时器,因为我以前从未这样做过。我希望有一个OnTimedEvents方法,我在一个单独的类中声明,因为我将在各种其他类中调用它进行各种其他测试,但我无法正确调用它而不会出现错误。具体来说,我得到的方法没有过载方法''取0个参数。我不能像其他方法那样解决这个问题。这是代码:
class MSOfficeApps {
public static Timer aTimer;
public void appWord() {
var programCS = new Program();
Microsoft.Office.Interop.Word.Application WordObj;
WordObj = (Microsoft.Office.Interop.Word.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Word.Application");
for(int i = 0; i < WordObj.Windows.Count; i++) {
object idx = i + 1;
Microsoft.Office.Interop.Word.Window WinObj = WordObj.Windows.get_Item(ref idx);
Console.WriteLine(WinObj.Document.FullName);
aTimer = new System.Timers.Timer(600000); //Sets timer to 6 minute increments
aTimer.Elapsed += ElapsedEventHandler(programCS.OnTimedEvent()); //Throwing an error at programCS.OnTimedEvent()
}
}
这就是我试图打电话
class Program {
private static void Main(string[] args) {
SearchProcesses sP = new SearchProcesses();
sP.BuildProcessLists();
Console.WriteLine("Press Enter to exit the program...");
Console.ReadLine();
}
public static void OnTimedEvent(Object source, ElapsedEventArgs e) {
Console.WriteLine("Event was raised at {0}", e.SignalTime);
}
}
请告知:)
答案 0 :(得分:0)
更改
aTimer.Elapsed += ElapsedEventHandler(programCS.OnTimedEvent());
到
aTimer.Elapsed += ElapsedEventHandler(programCS.OnTimedEvent);
您正在调用programCS.OnTimedEvent,而不是将其作为引用传递(并在不带参数的情况下调用它),从而导致错误。