user13248502
user13248502

Reputation:

How to run a process in the background of an HoloLens application?

Currently, my code can load and render an obj file from the hololens' local memory. However, during this process, the application is completely suspended, and this can take up to several seconds depending on the size and complexity of the mesh. Since one might suspect that the application crashed during this time, I want to move the rendering to a background process and keep the main thread active.

Current behaviour:

While loading and rendering an obj-file the application is suspended for a few seconds.

Desired behaviour:

The loading and rendering of the mesh is calculated in a background process and the app remains active.

Code snippet:

public class FileExplorer: MonoBehaviour {
    private FileOpenPicker openPicker;

    public void OpenExplorer() {
        UnityEngine.WSA.Application.InvokeOnUIThread(() = >OpenFileAsync(), false);
        LoadModelFromFile();
    }

    private async void OpenFileAsync() {
        openPicker = new FileOpenPicker();
        openPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
        openPicker.FileTypeFilter.Add(".obj");

        StorageFile file = await openPicker.PickSingleFileAsync();
        if (file != null) {
            path = file.Path;
        }
    }

    public void LoadModelFromFile() {
        Mesh newMesh = Instantiate(GetComponent < ObjFileLoader > ().LoadModel(path)) as Mesh;
    }
}

Notes:

If someone had a similar problem and could help me to solve this one or could give me a hint, I would be very grateful.

Upvotes: 0

Views: 729

Answers (1)

TJR
TJR

Reputation: 3733

I think what you are looking for is something like this.

public async Task OpenExplorer() {
    UnityEngine.WSA.Application.InvokeOnUIThread(async() => await OpenFileAsync(), false);
    await Task.Run(LoadModelFromFile);
}

My suggestion for you, though, is to read more on Async and Await because depending on your situation, the above code may not follow best practices and could be dangerous.

The main thing is that Async and Await is not about multi-threaded code. Async and Await are about using a single thread more efficiently. There are two main bottlenecks in code, IO, and CPU, and async await works well with IO bottlenecks because it can free up threads to work on other things while you wait for a disk operation or while you wait on a web request (which are not consuming any threads).

If you make a method async, but the only thing you are doing to make it async is kicking off a lot of long running code onto a different thread, it isn't that helpful unless the thread you are freeing up is the UI thread, but the only way you know that for sure is if you are on the UI thread.

So, only do await Task.Run... kind of stuff from a UI context when you need to free up the UI thread. Everywhere else, only label methods as async if they are purely IO related async like File.ReadAsync or others that do no consume any extra threads.

Upvotes: 0

Related Questions