multithreadingunit-testingsystem.reactivebetfair

Concurrent web request working in unit test but timing out when run from a form?


I'm using the library from here: https://github.com/joelpob/betfairng for concurrent web requests to the new Betfair API. I developed quite a bit of code using unit tests before I implemented a form to run everything.

The following code is from the example given, for me this will run when I run it from a unit test but will fail when I run it from a form (using a button click).

When I run it from a button press it will Login correctly but will time out on "ListCurrencyRates", this request will go through fine in the unit tests.

BetfairClient Client = new BetfairClient(Exchange.UK, "appKey");
MarketListener MarketListener = MarketListener.Create(Client, BFHelpers.HorseRacePriceProjection(), 1);

Client.Login("certificate", "certpass", "betfairUsername", "betfairPassword");

//*** times out here!!!!
var currencyRates = Client.ListCurrencyRates("GBP").Result; 

I haven't got an error or anything, I left it running for 30min and it didn't throw an error. When I paused the operation it just said "this is the next statement that will run when the thread returns from the current function".

Does anyone have any suggestions for why this could be happening?

Any help is greatly appreciated.

UPDATE

The following threads are running before the ListCurrencyRates is called from the unit test.

Not Flagged     9420    1   Main Thread Main Thread System.Threading.WaitHandle.WaitAny()   Normal
Not Flagged     12752   0   Worker Thread   <No Name>       Normal
Not Flagged     3792    5   Worker Thread   <No Name>       Normal
Not Flagged     7436    9   Worker Thread   <No Name>       Normal
Not Flagged     14396   0   Worker Thread   <No Name>       Normal
Not Flagged     14340   10  Worker Thread   <No Name>       Normal
Not Flagged     11268   11  Worker Thread   <No Name>       Normal
Not Flagged     14420   12  Worker Thread   <No Name>   System.Threading.Thread.Join()  Normal
Not Flagged     14532   14  Worker Thread   <No Name>   System.Threading.WaitHandle.WaitAny()   Normal
Not Flagged     13448   15  Worker Thread   <No Name>       Normal
Not Flagged     14564   17  Worker Thread   <No Name>       Normal
Not Flagged     14032   0   Worker Thread   <No Name>       Highest
Not Flagged     14356   16  Worker Thread   Worker Thread   BetfairNG.MarketListener.PollMarketBooks.AnonymousMethod__18    Normal
Not Flagged     14788   19  Worker Thread   EventPumpThread System.Threading.Monitor.Wait() Highest
Not Flagged >   14800   20  Worker Thread   TestRunnerThread    Core.Trader.GetMarkets  Normal
Not Flagged     15104   21  Worker Thread   <No Name>   System.Threading.WaitHandle.WaitAny()   Normal

And these are running at the same point when I run it by pressing the button in the form:

Not Flagged     0   0   Unknown Thread  [Thread Destroyed]      
Not Flagged     15340   0   Worker Thread   <No Name>       Highest
Not Flagged     11716   3   Worker Thread   <No Name>       Normal
Not Flagged     14448   8   Worker Thread   vshost.RunParkingWindow [Managed to Native Transition]  Normal
Not Flagged     15344   7   Worker Thread   <No Name>       Normal
Not Flagged     14656   10  Worker Thread   .NET SystemEvents   [Managed to Native Transition]  Normal
Not Flagged >   15016   11  Main Thread Main Thread Core.Trader.GetMarkets  Normal
Not Flagged     14512   6   Worker Thread   <No Name>       Normal
Not Flagged     9272    12  Worker Thread   Worker Thread   BetfairNG.MarketListener.PollMarketBooks.AnonymousMethod__18    Normal
Not Flagged     3588    0   Worker Thread   <No Name>       Normal
Not Flagged     13656   14  Worker Thread   <No Name>   System.Threading.WaitHandle.WaitAny()   Normal

Clearly there's a few missing, could this be the source of the problem?


Solution

  • I can't comment on the thread comparison but did notice that Pobar's sample is a console app which, like a test runner, executes on an MTA thread. Whereas a form (WinForms and WPF) is always STA-threaded. Maybe try executing your code on a threadpool thread, because these threads are always MTA?