Start async tasks on app start, await on later button click

-1

I have a Windows 10 UWP application that I want to start intialising some classes asap, and only later need them to be complete.

I am new to multithreaded programming so apologies for apparent stupidity of my approach.

I started a task in the App constructor, assigning the task to a static property:

sealed partial class App : Application {
    ...
    internal static Task CoreIntialisationTask { get; set; }

    public App() {
        this.InitializeComponent();

        CoreIntialisationTask = StartInitialisation();

        this.Suspending += OnSuspending;
    }

    private async Task StartInitialisation() {
        await InitialiseService1Async();
        var service2Task = this.InitializeService2Async();
        var service3Task = this.InitializeService3Async();
        await service2Task;
        await service3Task;
    }

    ...
}

Then in my first loaded page, on a button click, is the latest possible moment when these services need to be fully initialised, so I added a check status and await call:

private async void btnStart_Click(object sender, RoutedEventArgs e) 
{
    if (App.CoreIntialisationTask.Status == TaskStatus.Running)
    {
        await App.CoreIntialisationTask;
        ...
    }
    ...
}

However, the CoreIntialisationTask Status at this point is Failure with the following inner Exception:

(new System.Threading.Tasks.SystemThreadingTasks_FutureDebugView<System.Threading.Tasks.VoidTaskResult>(App.CoreIntialisationTask).Exception).InnerException.Message
The application called an interface that was marshalled for a different thread. (Exception from HRESULT: 0x8001010E (RPC_E_WRONG_THREAD))

at Windows.UI.Xaml.Application.get_Resources()
at MyApplication.App.<InitializeService>d__26.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
at MyApplication.App.<StartInitialisation>d__25.MoveNext()

The StartInitialisation() method seems to get to the await service2Task but never the await service3Task; line.

Am I approaching this completely the wrong way or is there something more minor I am missing here?

c#
multithreading
uwp
asked on Stack Overflow Jul 14, 2017 by monty • edited Jul 14, 2017 by Salih Karagoz

1 Answer

0

You need to check server status or process.
If you need server you can use like:

Public Sub StratServices(ByVal servername As String)
    Try
        Dim service As ServiceController = New ServiceController(servername)
        If service.Status = ServiceControllerStatus.Stopped Then
            service.Start()
            service.Refresh()
        End If
    Catch ex As Exception
        MsgBox("You dont have Sql Srver in your Machine")
    End Try
End Sub

By use System.Serviceprocess
answered on Stack Overflow Jul 14, 2017 by Ayman Mubark • edited Jul 14, 2017 by Dovydas Ĺ opa

User contributions licensed under CC BY-SA 3.0