Skip to content

Commit

Permalink
srtm: async improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
meee1 committed Jun 19, 2021
1 parent a373ab6 commit ac415f5
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 19 deletions.
35 changes: 21 additions & 14 deletions ExtLibs/Utilities/srtm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,7 @@ static async void requestRunner()
{
try
{
await requestSemaphore.WaitAsync(30000);
await requestSemaphore.WaitAsync(30000).ConfigureAwait(false);

string item = "";
lock (objlock)
Expand All @@ -568,7 +568,7 @@ static async void requestRunner()
if (item != "")
{
log.Info(item);
await get3secfile(item);
await get3secfile(item).ConfigureAwait(false);
lock (objlock)
{
queue.Remove(item);
Expand All @@ -587,7 +587,14 @@ static async void requestRunner()
}

// never more than 1/s
await Task.Delay(1000);
try
{
await Task.Delay(1000).ConfigureAwait(false);
}
catch
{

}
}
}

Expand All @@ -605,17 +612,17 @@ static async Task get3secfile(object name)
List<string> list = new List<string>();

// load 1 arc seconds first
list.AddRange(await getListing(baseurl1sec));
list.AddRange(await getListing(baseurl1sec).ConfigureAwait(false));
log.Info("srtm1sec " + list.Count);
// load 3 arc second
list.AddRange(await getListing(baseurl));
list.AddRange(await getListing(baseurl).ConfigureAwait(false));
log.Info("srtm1esc+3sec " + list.Count);

foreach (string item in list)
{
List<string> hgtfiles = new List<string>();

hgtfiles = await getListing(item);
hgtfiles = await getListing(item).ConfigureAwait(false);

foreach (string hgt in hgtfiles)
{
Expand All @@ -624,7 +631,7 @@ static async Task get3secfile(object name)
{
// get file

await gethgt(hgt, (string) name);
await gethgt(hgt, (string) name).ConfigureAwait(false);
return;
}
}
Expand All @@ -651,19 +658,19 @@ static async Task gethgt(string url, string filename)
{
log.Info("Get " + url);

using (var res = await client.GetAsync(url))
using (Stream resstream = await res.Content.ReadAsStreamAsync())
using (var res = await client.GetAsync(url).ConfigureAwait(false))
using (Stream resstream = await res.Content.ReadAsStreamAsync().ConfigureAwait(false))
using (
BinaryWriter bw =
new BinaryWriter(File.Create(datadirectory + Path.DirectorySeparatorChar + filename + ".zip")))
{
byte[] buf1 = new byte[1024];
byte[] buf1 = new byte[1024*4];

int size = 0;

while (resstream.CanRead)
{
int len = await resstream.ReadAsync(buf1, 0, 1024);
int len = await resstream.ReadAsync(buf1, 0, buf1.Length).ConfigureAwait(false);
if (len == 0)
break;
bw.Write(buf1, 0, len);
Expand Down Expand Up @@ -722,11 +729,11 @@ static async Task<List<string>> getListing(string url)
{
log.Info("srtm req " + url);

using (var res = await client.GetAsync(url))
using (StreamReader resstream = new StreamReader(await res.Content.ReadAsStreamAsync()))
using (var res = await client.GetAsync(url).ConfigureAwait(false))
using (StreamReader resstream = new StreamReader(await res.Content.ReadAsStreamAsync().ConfigureAwait(false)))
{

string data = await resstream.ReadToEndAsync();
string data = await resstream.ReadToEndAsync().ConfigureAwait(false);

Regex regex = new Regex("href=\"([^\"]+)\"", RegexOptions.IgnoreCase);
if (regex.IsMatch(data))
Expand Down
11 changes: 6 additions & 5 deletions MainV2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2813,7 +2813,9 @@ private async void SerialReader()
linkqualitytime = DateTime.Now;

// force redraw if there are no other packets are being read
GCSViews.FlightData.myhud.Invalidate();
this.BeginInvokeIfRequired(
(Action)
delegate { GCSViews.FlightData.myhud.Invalidate(); });
}
}

Expand Down Expand Up @@ -2858,14 +2860,13 @@ private async void SerialReader()
if (MyView.current != null && MyView.current.Name == "FlightPlanner")
{
// update home if we are on flight data tab
this.BeginInvoke((Action) delegate { FlightPlanner.updateHome(); });
this.BeginInvokeIfRequired((Action) delegate { FlightPlanner.updateHome(); });
}

}
catch
{
// dont hang this loop
this.BeginInvoke(
this.BeginInvokeIfRequired(
(Action)
delegate
{
Expand Down Expand Up @@ -3254,7 +3255,7 @@ protected override void OnLoad(EventArgs e)
// update firmware version list - only once per day
ThreadPool.QueueUserWorkItem(BGFirmwareCheck);

ThreadPool.QueueUserWorkItem(async (s) =>
Task.Run(async () =>
{
try
{
Expand Down

0 comments on commit ac415f5

Please sign in to comment.