Skip to content

Commit

Permalink
[Xamarin.Android.Build.Tasls] Rework AsyncTask stuff a bit
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
dellis1972 committed Nov 9, 2017
1 parent fcdb347 commit 96c885c
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 6 deletions.
7 changes: 6 additions & 1 deletion src/Xamarin.Android.Build.Tasks/Tasks/Aapt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,12 @@ public override bool Execute ()

assemblyMap.Load (AssemblyIdentityMapFile);

ThreadingTasks.Parallel.ForEach (ManifestFiles, () => 0, DoExecute, (obj) => { Complete (); });
ThreadingTasks.ParallelOptions options = new ThreadingTasks.ParallelOptions {
CancellationToken = Token,
TaskScheduler = ThreadingTasks.TaskScheduler.Current,
};

ThreadingTasks.Parallel.ForEach (ManifestFiles, options, () => 0, DoExecute, (obj) => { Complete (); });

base.Execute ();

Expand Down
5 changes: 3 additions & 2 deletions src/Xamarin.Android.Build.Tasks/Tasks/Aot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -239,11 +239,11 @@ bool DoExecute () {

var task = ThreadingTasks.Task.Run ( () => {
return RunParallelAotCompiler (nativeLibs);
});
}, Token);

task.ContinueWith ( (t) => {
Complete ();
});
}).ConfigureAwait (false);

base.Execute ();

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

ThreadingTasks.ParallelOptions options = new ThreadingTasks.ParallelOptions {
CancellationToken = cts.Token,
TaskScheduler = ThreadingTasks.TaskScheduler.Default,
};

ThreadingTasks.Parallel.ForEach (GetAotConfigs (), options,
Expand Down
7 changes: 6 additions & 1 deletion src/Xamarin.Android.Build.Tasks/Tasks/Crunch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,14 @@ public override bool Execute ()
if (!imageFiles.Any ())
return true;

ThreadingTasks.ParallelOptions options = new ThreadingTasks.ParallelOptions {
CancellationToken = Token,
TaskScheduler = ThreadingTasks.TaskScheduler.Current,
};

var imageGroups = imageFiles.GroupBy (x => Path.GetDirectoryName (Path.GetFullPath (x.ItemSpec)));

ThreadingTasks.Parallel.ForEach (imageGroups, () => 0, DoExecute, (obj) => { Complete (); });
ThreadingTasks.Parallel.ForEach (imageGroups, options, () => 0, DoExecute, (obj) => { Complete (); });

return !Log.HasLoggedErrors;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -427,11 +427,11 @@ public override bool Execute ()
}
}
}
}).ContinueWith ((t) => {
}, Token).ContinueWith ((t) => {
if (t.Exception != null)
Log.LogErrorFromException (t.Exception.GetBaseException ());
Complete ();
});
}).ConfigureAwait (false);

var result = base.Execute ();

Expand Down

0 comments on commit 96c885c

Please sign in to comment.