-
Notifications
You must be signed in to change notification settings - Fork 37
Loading files
SwissEphNet is a Portable Classe Library and we don't have file access.
As Swiss Ephemeris use some data files, an event exists for loading the files required.
using (var sweph = new SwissEphNet.SwissEph()) {
sweph.OnLoadFile += (s, e) => {
// Loading file
};
// Use it
}
The LoadFileEventArgs
contains :
-
FileName
: Full name of the file required. -
File
: ASystem.IO.Stream
object of the file.
If the file not exists leave LoadFileEventArgs.FileName
as null
.
By default the search paths is "[ephe]", so you can detect the default search by this name.
If you define the ephemeris path with SwissEphNet.SwissEph.swe_set_ephe_path()
the search works like the original Swiss Ephemeris (http://www.astro.com/swisseph/swephprg.htm#_Toc379890481).
Some type of application like UWP or Universal (Windows 8.1/Windows Phone 8.1) are access files in an async context only.
Because the SwissEphNet Library use an event to load the files, we can't used await/async
because it's a void
and the method don't wait the end of the load process.
The only solution is to wait the task, but there is a problem.
For example with this method for open a file
async Task<Stream> OpenFileAsync(string filename) { await ...; }
and we make the event Handler like this:
void OnLoadFile(object s, LoadFileEventArgs e) {
var tsk = OpenFileAsync(e.FileName);
tsk.Wait();
e.File = tsk.Result;
// Could be writed as: e.File = OpenFileAsync(e.FileName).Result;
};
The problem with this code is the using of the TPL Task.Wait()
method wich use a different sync context than the await/async
call in the same thread. This cause an interlock and freeze your application.
The solution is to change the thread to change the context. To do this, we need to change our code like this:
void OnLoadFile(object s, LoadFileEventArgs e) {
e.File = Task.Run(async () => await OpenFileAsync(e.FileName)).Result;
};
We create a new thread with Task.Run()
so we use a new context for the await/async
call, no interlock created.
The problem with this solution is it used a new thread in the ThreadPool, that could be a problem in a small resources environment (like phone).