Search code examples
c#mvvmstartup

Best way to call startup tasks in WPF/MVVM


I have a few tasks that I handle on the startup of my WPF/MVVM application. Is there any reason I can't call these tasks from my app.xaml.cs class? Not sure if that goes against MVVM or if this is a best practise way to handle startup tasks.

Examples of the types of tasks i'll be calling:

  1. Checking for the db and creating it if it does not exist
  2. Checking for the app settings that are needed on startup, creating them with default data if not found
  3. Etc...

I've tried it a few ways and each work, but I'm curious what standard people follow. I've experimented with the following.

  1. Calling them from my MainViewModel
  2. Calling them from app.xaml.cs
  3. Created them as services and Injected them into my MainViewModel

Solution

  • I suggest you not to include your startup tasks in application make separate application in WPF/MVVM and create a separate windows service for your startup task and access this service in your application.

    1. This separation make your code cleaner and easy to maintained.
    2. If startup tasks goes down then you can show message on application. To start the service.
    3. you can start/stop service independently.
    4. If startup task took more time to start this will for one time only . End user will not deal with this time he only see your application UP time.