C# と VB.NET の質問掲示板

わんくま同盟

ASP.NET、C++/CLI、Java 何でもどうぞ

C# と VB.NET の入門サイト


(過去ログ 48 を表示中)
■26129 / )  C# Windowsサービス、スレッド、デストラクタについて
□投稿者/ 直江兼続 (1回)-(2008/10/02(Thu) 14:19:58)

分類:[C#] 

お世話になります。

C#のWindowsサービスにて不明な点があるため、
投稿させて頂きました。

サービスをスタートさせるタイミングで5つのスレッドを起動させます。
スレッド内では常に監視の処理を行っている状態のまま、
サービスで停止を行う際、スレッド内に設けてあるデストラクタを起動させ、
かつスレッド自体のインスタンスも破棄したいのですが、
何かうまいやり方はありますでしょうか?

サービス側のOnStop()メソッド内の記述をお願い致します。



using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;
using System.Threading;
using Client;
namespace Service
{
public partial class Service1 : ServiceBase
{
private Thread thread_; // スレッド
private Thread[] threads; // スレッド配列

public Service1()
{
InitializeComponent();

// スレッド配列(5スレッド)作成
threads = new Thread[5];
}

protected override void OnStart(string[] args)
{
string aaa = "あああ";
string bbb = "いいい";

for (int i = 0; i < 4; i++)
{
// スレッド作成
Client1 aClient_ = new Client1(aaa, bbb);
thread_ = new Thread(new ThreadStart(aClient_.ThreadMethod));

// スレッド開始
thread_.Start();

threads[i] = thread_;
}
}

protected override void OnStop()
{
///******************************
///???????????????
///******************************
}
}
}



using System;
using System.Collections.Generic;
using System.Text;
namespace Client
{
public class Client1
{
private string aaa;
private string bbb;

public Client1(string a, string b)
{
this.aaa = a;
this.bbb = b;
}

~Client1()
{
///---------------------------
///終了処理
///---------------------------
}

public void ThreadMethod()
{
bool isContinue = true; // ループフラグ

// trueの間ループ
while (isContinue)
{
///---------------------------
///監視処理
///---------------------------
}
}
}
}

返信 編集キー/


管理者用

- Child Tree -