Can somebody explain this odd behavior when working with ThreadPool?

后端 未结 2 1532
南方客
南方客 2020-12-04 02:17

The Code

using System;
using System.Threading;

public delegate void LoadingProgressCallback(double PercentComplete,string ItemName);
public delegate void          


        
2条回答
  •  旧时难觅i
    2020-12-04 02:46

    One thing that immediately comes to mind looking at the code is lack of use of Interlocked.

    You have to use it otherwise you will see strange errors and behaviours.

    So instead of

    numThreads++;
    

    Use:

    Interlocked.Increment(ref numThreads);
    

提交回复
热议问题