Вопрос

У меня есть нить, которая вызывает два отдельных потока, чтобы сделать кажушки.Всякий раз, когда какая-либо из заданий завершается WATHANDLE.SET (0 называется и в конце родительской рабочей нити, которую я хотел, чтобы оейнал, чтобы заканчиваться, прежде чем я продолжу. Но Pricea () все еще подходит первым, а затем Priceb().

new Thread(() =>
                           {
                               new Thread(() =>
                               {
                                   PriceA = _service.GetPriceA();
                                   _waithandle[0].Set();
                               }).Start();

                               new Thread(() =>
                               {
                                   PriceB = _service.GetPriceB();
                                   _waithandle[1].Set();
                               }).Start();

                               WaitHandle.WaitAll(_waithandle);
                           }).Start();
Console.WriteLine("Hello");
.

Что я скучаю?

<Сильное> Обновление:

private EventWaitHandle[] _waithandle;
.

<Сильный> CTOR:

 _waithandle[0] = new ManualResetEvent(false);
 _waithandle[1] = new ManualResetEvent(false);
.

Это было полезно?

Решение 2

new Thread(() =>
                           {
                               new Thread(() =>
                               {
                                   _priceA = _service.GetPriceA();
                                   _waithandle[0].Set();
                               }).Start();

                               new Thread(() =>
                               {
                                   _priceB = _service.GetPriceB();
                                   _waithandle[1].Set();
                               }).Start();

                               WaitHandle.WaitAll(_waithandle);
                               PriceA = _priceA;
                               PriceB = _priceB;
                           }).Start();

This did the job for me. Culprit was the INotifyChangedProperty() of the PriceA and PriceB which updated the UI too early making my waitall redundant. In case someone else has a similar issue...

Другие советы

You're creating a separate thread to wait... but the code after the statement you've given will continue because you're not waiting in that thread. In other words, you're creating three threads:

  • Thread X: Creates threads A and B, then waits for them to finish
  • Thread A: Gets PriceA and then sets waitHandle[0]
  • Thread B: Gets PriceB and then sets waitHandle[1]

But thread X is doing nothing after it's waited, so what's the point of waiting within it?

Additionally, it would be a lot simpler to just call Join on the extra threads that you've created. In fact, if you only need to wait in the "current" thread, you only need one extra thread in the first place:

Thread t = new Thread(() => { PriceA = _service.GetPriceA(); });
t.Start();
PriceB = _service.GetPriceB();
t.Join();
// Other code

By the time you reach "other code", both PriceA and PriceB will have been set. Of course, this is missing a considerable amount of error handling... but that's easier to add when you've got a simpler starting point than your currently over-complicated code.

Are you resetting correctly the _waithandle[0] and [1]? For example:

_waithandle[0] = new ManualResetEvent(false);
_waithandle[1] = new ManualResetEvent(false);
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top