static EventWaitHandle waitHandle = new AutoResetEvent(false);
private async Task StartTrade(CancellationToken ct)
{
_ = OandaJsonStream(ct);
waitHandle.WaitOne();
Console.WriteLine("HEY");
}
private async Task DeserializeFromStream(CancellationToken ct)
{
try
{ /// waitHandle.Set(); It works if I place it here
using (var client = new HttpClient())
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Add("APP_VERSION", "1.0.0");
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", "3c804286df0f9a3151c0452719c0fb5c-d19af58209cb567d40eefe605f8b30f5");
using (var request = new HttpRequestMessage(HttpMethod.Get, "https://stream-fxpractice.oanda.com/v3/accounts/101-004-4455670-001/pricing/stream?instruments=EUR_USD"))
using (var response = await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, ct))
{
var stream = await response.Content.ReadAsStreamAsync();
using (StreamReader sr = new StreamReader(stream))
using (JsonTextReader reader = new JsonTextReader(sr))
{
reader.SupportMultipleContent = true;
await Task.Run(() =>
{
while (reader.Read())
{
if (reader.TokenType == JsonToken.StartObject)
{
JObject obj = JObject.Load(reader);// Load each object from the stream and do something with it
Dispatcher.BeginInvoke((Action)(() => { debugOutput(obj["time"].ToString()); }));
waitHandle.Set(); // It does not work if I place it here!
Console.WriteLine(obj);
} ct.ThrowIfCancellationRequested();
}
}, ct);
}
}
}
catch (HttpRequestException ex) {System.Windows.MessageBox.Show("Error. " + ex.Message);}
catch (OperationCanceledException) {System.Windows.MessageBox.Show("Your submission was canceled.");}
}
Если я помещу строку "waitHandle.Set ();" сразу после «try {» это работает, но если я помещаю строку в l oop или где-либо еще после «using», это не сигнализирует. Может кто-нибудь помочь мне, пожалуйста. Спасибо.