可以使用我的计时器包装类,那就太好了

发布于 2024-11-28 13:12:02 字数 1769 浏览 1 评论 0原文

我至少已经缩小了在这个问题上的错误,但现在我陷入了困境。我认为“它似乎应该有效”,并且我只重新找到相同的帖子。所以。我正在使用一个抽象基类,我想用它来控制数据下载过程。在这个类中,我添加了一个为 System.Threading.Timer 编写的包装器实例。在我的计时器的构造函数中,我有两个参数,一个是尝试将抽象方法传递给回调对象,另一个是计时器的滴答事件的时间,基于具体/派生类中的实例变量不同的数据下载过程。

所以这是我的代码:

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);
    }

}

我的错误:

参数 1:无法从 'void' 转换为 'System.Threading.TimerCallback'

方法 'MainDataRunProcedure' 的 DataOperations 重载需要 0 个参数

'DataOperations.DataRunTimer.DataRunTimer(System) 的最佳重载方法匹配.Threading.TimerCallback, int)' 有一些无效参数

将期待任何建议。谢谢...

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/ derived 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...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

聚集的泪 2024-12-05 13:12:02

编译器错误非常不言自明:MainDataRunProcedure 返回 void,但您尝试使用其返回值来调用 DataRunTimer构造函数。

如果您尝试传递方法本身(在本例中为 MainDataRunProcedure),请删除括号,即:

new DataRunTimer(MainDataRunProcedure, GetMillisecondsUntilDataRun());

省略括号会将该方法作为委托传递。如果您添加括号,那么您实际上是在尝试调用该方法(在本例中不带参数)。

此外,抽象静态方法既非法又毫无意义。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文