I've at least narrowed my errors on this issue, but I'm now just stuck. I'm at that "it just seems like it should work" and I'm refinding the same posts only. So. I'm working with an abstract base class that I want to use to control data download processes. To this class I've added an instance of a wrapper I wrote for System.Threading.Timer. In the constructor for my timer, I have two parameters, one is an attempt to pass an abstract method to the Callback object, the other the time for the tick event of the timer, based on an instance variable in the concrete/ derive开发者_JS百科d classes for the different data download processes.
So here's my code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Threading;
using HtmlAgilityPack;
using DataUtlitiesMain;
using TradeSystemObjects;
using System.Data.SqlClient;
namespace DataOperations
{
public abstract class DataRunManager
{
DataRunTimer dataRunTimer;
private abstract String HourForDataRunInMilitary; //Such as 16.0 for 4pm.
public DataRunManager()
{
SetDataRunTimer();
}
private void SetDataRunTimer()
{
dataRunTimer = new DataRunTimer(MainDataRunProcedure(),GetMillisecondsUntilDataRun());
}
protected abstract static void MainDataRunProcedure(object state);
//public abstract TimerCallback SetDataRunMainDelegate();
private Int32 GetMillisecondsUntilDataRun()
{
DateTime now = DateTime.Now;
DateTime targetTime = DateTime.Today.AddHours(Convert.ToDouble(HourForDataRunInMilitary));
return (int)((targetTime - now).TotalMilliseconds);
}
}
public class DataRunTimer
{
Timer timer;
public DataRunTimer(TimerCallback TimerProc, Int32 MillisecondsUntilDataRun)
{
Timer timer = new Timer(TimerProc, null, MillisecondsUntilDataRun, 1000);
}
}
And my errors:
Argument 1: cannot convert from 'void' to 'System.Threading.TimerCallback' DataOperations
overload for method 'MainDataRunProcedure' takes 0 arguments
The best overloaded method match for 'DataOperations.DataRunTimer.DataRunTimer(System.Threading.TimerCallback, int)' has some invalid arguments
Will look forward to any suggestions. Thanks...
The compiler error is pretty self-explanatory: MainDataRunProcedure
returns void
, yet you're trying to use its return value for the call to the DataRunTimer
constructor.
If you're trying to pass the method itself (MainDataRunProcedure in this case), then remove the parentheses, i.e.:
new DataRunTimer(MainDataRunProcedure, GetMillisecondsUntilDataRun());
Omitting the parentheses passes that method as a delegate. If you put parentheses, then you're actually trying to invoke the method (with no parameters, in this case).
Also, abstract static
methods are both illegal and meaningless.
精彩评论