I'm trying to understand a general flow of WPF application, it's not everything clear for me, so, please, help me to understand the following:
If I want my database polling Timer in a WPF application was started not from a code behind file of my main window, but from another separate class, how I should implement this?
Should this class be somehow connected with a main window code behind class? What processes (apart of user actions) can instantiate this separate class and what (apart of user actions) can call the method which starts a Timer?
In the place of this Timer can be any Method, it is important to understand the general architectural principle of application flow and class objects structure.
WPF applications have by default an
App class that is called first. If you don't want to connect your timer to the main window (what is your reason?) you can start your timer in the application's
Startup event handler.
public partial class App : Application
private void Application_Startup(object sender, StartupEventArgs e)
// Instantiate your class and start the timer
and in the App.xaml file:
<Application ... Startup="Application_Startup">...</Application>
In response to your comment:
I'd use a
BackgroundWorker to execute the database-polling method in the background. You can then kick that method off with a timer or with a control in the UI, without worrying that you're touching the UI thread inappropriately. If you encapsulate this in a class, you can bind a command to the method that launches it and do away with code-behind entirely.