Skip to content

Commit 7d77a39

Browse files
committed
[Xamarin.Android.Build.Tasks] Rework AsyncTask to use ConfigureAwait
We should be using `ConfigureAwait(false)` for our `Task.Run` calls to ensure that the Continuations do NOT run on the main thread. This is probably the reason why VS locks up when we call these methods. The Continuation which calls `Complete` is trying to run on the UI thread, which will be locked waiting for the Task to complete. We should also provide the Cancelation Token and the TaskScheduler for the `Parallel.ForEach` calls.
1 parent fcdb347 commit 7d77a39

File tree

4 files changed

+21
-8
lines changed

4 files changed

+21
-8
lines changed

src/Xamarin.Android.Build.Tasks/Tasks/Aapt.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,12 @@ public override bool Execute ()
216216

217217
assemblyMap.Load (AssemblyIdentityMapFile);
218218

219-
ThreadingTasks.Parallel.ForEach (ManifestFiles, () => 0, DoExecute, (obj) => { Complete (); });
219+
ThreadingTasks.ParallelOptions options = new ThreadingTasks.ParallelOptions {
220+
CancellationToken = Token,
221+
TaskScheduler = ThreadingTasks.TaskScheduler.Current,
222+
};
223+
224+
ThreadingTasks.Parallel.ForEach (ManifestFiles, options, () => 0, DoExecute, (obj) => { Complete (); });
220225

221226
base.Execute ();
222227

src/Xamarin.Android.Build.Tasks/Tasks/Aot.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,11 +239,11 @@ bool DoExecute () {
239239

240240
var task = ThreadingTasks.Task.Run ( () => {
241241
return RunParallelAotCompiler (nativeLibs);
242-
});
242+
}, Token);
243243

244244
task.ContinueWith ( (t) => {
245245
Complete ();
246-
});
246+
}).ConfigureAwait (false);
247247

248248
base.Execute ();
249249

@@ -267,6 +267,7 @@ bool RunParallelAotCompiler (List<string> nativeLibs)
267267

268268
ThreadingTasks.ParallelOptions options = new ThreadingTasks.ParallelOptions {
269269
CancellationToken = cts.Token,
270+
TaskScheduler = ThreadingTasks.TaskScheduler.Current,
270271
};
271272

272273
ThreadingTasks.Parallel.ForEach (GetAotConfigs (), options,

src/Xamarin.Android.Build.Tasks/Tasks/Crunch.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,14 @@ public override bool Execute ()
8080
if (!imageFiles.Any ())
8181
return true;
8282

83+
ThreadingTasks.ParallelOptions options = new ThreadingTasks.ParallelOptions {
84+
CancellationToken = Token,
85+
TaskScheduler = ThreadingTasks.TaskScheduler.Current,
86+
};
87+
8388
var imageGroups = imageFiles.GroupBy (x => Path.GetDirectoryName (Path.GetFullPath (x.ItemSpec)));
8489

85-
ThreadingTasks.Parallel.ForEach (imageGroups, () => 0, DoExecute, (obj) => { Complete (); });
90+
ThreadingTasks.Parallel.ForEach (imageGroups, options, () => 0, DoExecute, (obj) => { Complete (); });
8691

8792
return !Log.HasLoggedErrors;
8893
}

src/Xamarin.Android.Build.Tasks/Tasks/GetAdditionalResourcesFromAssemblies.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -427,11 +427,13 @@ public override bool Execute ()
427427
}
428428
}
429429
}
430-
}).ContinueWith ((t) => {
431-
if (t.Exception != null)
432-
Log.LogErrorFromException (t.Exception.GetBaseException ());
430+
}, Token).ContinueWith ((t) => {
431+
if (t.Exception != null) {
432+
var ex = t.Exception.GetBaseException ();
433+
LogError (ex.Message + Environment.NewLine + ex.StackTrace);
434+
}
433435
Complete ();
434-
});
436+
}).ConfigureAwait (false);
435437

436438
var result = base.Execute ();
437439

0 commit comments

Comments
 (0)