假设螺纹形成4个单独的螺纹并等待它们中的每一个直到它们完成.每个线程都会休眠一段时间,只有当共享的Mutex opbject没有被另一个线程占用时才会终止,然后通过它完成的事件发出信号(这是我的代码的简化版本,但在同一个地方失败)
但是,大多数情况下主线程将在WaitOne()中的一个看似随机等待.
此外,我不得不将我的代码的某些部分注释掉,因为它导致了更多的意外行为(也就是说,在每个线程完成主线程之后会以某种方式跳回到for子句并导致IndexOutOfBounds)
class Threading
{
static Mutex CM;
static List<Manga> SharedList;
static ManualResetEvent CEvent = new ManualResetEvent(false);
static ManualResetEvent Event1 = new ManualResetEvent(false);
static ManualResetEvent Event2 = new ManualResetEvent(false);
static ManualResetEvent Event3 = new ManualResetEvent(false);
static ManualResetEvent Event4 = new ManualResetEvent(false);
public List<Manga> ThreadedMangaIndexCrawl(int MaxThreads)
{
CM = new Mutex(false);
SharedList = new List<Manga>();
ManualResetEvent[] evs = new ManualResetEvent[4];
evs[0] = Event1; // Event for t1
evs[1] = Event2; // Event for t2
evs[2] = Event3; // Event …Run Code Online (Sandbox Code Playgroud)