0

OnStart Evenを使用して次のことを行う vb.net を使用して Windows サービスを開発しました...

  • SQL テーブルからすべてのエントリを取得します
  • 返された行からスケジュールを作成します

それはうまく機能し、時間などに発砲をスケジュールします。

問題:テーブルに新しい行を追加する必要があるときはいつでも、サービスを再起動する必要があるため、新しく作成された行を取得できます。これは私にとって問題を引き起こしています...すでに実行されているタスクがあり、サービスを再起動するとシステムが壊れる可能性があります。

では、これを処理する最善の方法は何ですか? 再起動せずに新しい行をサービスにロードできますか?

ありがとう

4

2 に答える 2

1

データベースへのポーリングの概念を使用します。System.Threading.Timerクラスを使用して、コールバックメソッドが呼び出され、データベースに新しいエントリをポーリングするようになるまでの間隔を設定します。

于 2012-11-29T03:47:09.000 に答える
0

この OnStart は、Marc Gravell によって提供されました。

public void OnStart(string[] args) // should this be override?
{
    var worker = new Thread(DoWork);
    worker.Name = "MyWorker";
    worker.IsBackground = false;
    worker.Start();
}
void DoWork()
{
    // do long-running stuff
}

複数のスレッドを起動できることに注意してください。OnStartまたは、最初に起動されたスレッドを使用して、必要に応じて追加のスレッドを起動できます。これにより、データベース ポーリングまたはメッセージ キューでデータを待機するスレッドを設定できます。


役立つヒント:

サービスに を追加するMainと、Visual Studio でコンソール アプリケーションとして実行できます。これにより、デバッグが大幅に簡素化されます。

    static void Main(string[] args)
    {
        ServiceTemplate service = new ServiceTemplate();
        if (Environment.UserInteractive)
        {
            // The application is running from a console window, perhaps creating by Visual Studio.
            try
            {
                if (Console.WindowHeight < 10)
                    Console.WindowHeight = 10;
                if (Console.WindowWidth < 240) // Maximum supported width.
                    Console.WindowWidth = 240;
            }
            catch (Exception)
            {
                // We couldn't resize the console window.  So it goes.
            }

            service.OnStart(args);
            Console.Write("Press any key to stop program: ");
            Console.ReadKey();
            Console.Write("\r\nInvoking OnStop() ...");
            service.OnStop();

            Console.Write("Press any key to exit: ");
            Console.ReadKey();
        }
        else
        {
            // The application is running as a service.
            // Misnomer.  The following registers the services with the Service Control Manager (SCM).  It doesn't run anything.
            ServiceBase.Run(service);
        }
    }
于 2012-11-29T04:15:40.577 に答える