0

Thread をバックグラウンド スレッドに設定したいのですが、このプロパティが Thread にないのはなぜですか?

    ThreadStart starter = delegate { openAdapterForStatistics(_device); };
    new Thread(starter).Start();

public void openAdapterForStatistics(PacketDevice selectedOutputDevice)
{
    using (PacketCommunicator statCommunicator = selectedOutputDevice.Open(100, PacketDeviceOpenAttributes.Promiscuous, 1000)) //open the output adapter
    {
        statCommunicator.Mode = PacketCommunicatorMode.Statistics; //put the interface in statstics mode                
        statCommunicator.ReceiveStatistics(0, statisticsHandler);

    }
}

私は試してみました:

Thread thread = new Thread(openAdapterForStatistics(_device));

しかし、私は2つのコンパイルエラーを持っています:

  1. 'System.Threading.Thread.Thread(System.Threading.ThreadStart)' に最も一致するオーバーロードされたメソッドには、無効な引数が含まれています
  2. 引数 1: 'void' から 'System.Threading.ThreadStart' に変換できません

そして、私は理由を知りません

4

3 に答える 3

1

背景については、スレッドへの参照を保持していないため、設定方法がわかりません。次のようになります。

ThreadStart starter = delegate { openAdapterForStatistics(_device); };
Thread t = new Thread(starter);
t.IsBackground = true;
t.Start();

これ

Thread thread = new Thread(openAdapterForStatistics(_device));

メソッド呼び出しobjectの結果を実際に渡しているときに、パラメーターとして受け取るメソッドを渡すことになっているため、機能しません。だからあなたはこれを行うことができます:

public void openAdapterForStatistics(object param)
{
    PacketDevice selectedOutputDevice = (PacketDevice)param;
    using (PacketCommunicator statCommunicator = selectedOutputDevice.Open(100, PacketDeviceOpenAttributes.Promiscuous, 1000)) //open the output adapter
    {
        statCommunicator.Mode = PacketCommunicatorMode.Statistics; //put the interface in statstics mode                
        statCommunicator.ReceiveStatistics(0, statisticsHandler);

    }
}

と:

Thread t = new Thread(openAdapterForStatistics);
t.IsBackground = true;
t.Start(_device);
于 2012-10-04T10:22:50.690 に答える
0
PacketDevice selectedOutputDeviceValue = [some value here];
Thread wt = new Thread(new ParameterizedThreadStart(this.openAdapterForStatistics));
wt.Start(selectedOutputDeviceValue);
于 2012-10-04T10:32:59.183 に答える
0

BackgroundWorkerあなたの場合のように使用するために特別に設計されたクラスを使用する必要があります。バックグラウンドで実行したいタスク。

于 2012-10-04T10:22:53.573 に答える