0

List<MyClass>N個のアイテムがあります。私は次のコードで処理します

foreach (var item in myList)
{
    ThreadPool.QueueUserWorkItem(DoWorkWithItem, item);
}

private void DoWorkWithItem(object threadContext)
{
   ///...
}

しかし、どのアイテムが処理されたかをレポートする必要があります。このタスクのイベントなどを取得できます。

4

1 に答える 1

0

次のように、イベントを作成し、脅威の反復が終了した後に呼び出します。

public class ClassWithDelegate
{
    public delegate void ProgressReportEventHandler();
    public static ProgressReportEventHandler ProgressReport { get; set; };

    private void DoWorkWithItem(object threadContext)
    {
       // angry code

       if (ProgressReport != null)
          ProgressReport();
    }
}

public class Subscriber
{
    public Subscriber()
    {
         ClassWithDelegate.ProgressReport += ProgressReport;         
    }

    public void ProgressReport()
    {
        //todo
    }
}

または使用するBackgroundWorkerと、このイベントが発生します

于 2012-07-19T15:23:31.837 に答える