A possibility is to use the OnStartup function in the App.xaml.cs file. For the asynchronous call, various methods exist, following some methods to make an asynchronous call.
Using BackgroundWorker
public partial class App : Application
{
protected override void OnStartup(System.Windows.StartupEventArgs e)
{
BackgroundWorker _backgroundWorker;
if (_backgroundWorker != null)
{
_backgroundWorker.CancelAsync();
}
_backgroundWorker = CreateBackgroundWorker();
_backgroundWorker.RunWorkerAsync();
}
}
private BackgroundWorker CreateBackgroundWorker()
{
var bw = new BackgroundWorker();
bw.WorkerReportsProgress = true;
bw.WorkerSupportsCancellation = true;
bw.DoWork += _backgroundWorker_DoWork;
bw.ProgressChanged += _backgroundWorker_ProgressChanged;
bw.RunWorkerCompleted += _backgroundWorker_RunWorkerCompleted;
return bw;
}
private void _backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
}
private void _backgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
}
private void _backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
}
Using Thread
Method 1
myThread = new Thread(() => CustomTaskAsync());
myThread.Start();
private void CustomTaskAsync()
{
...............
}
Method 2
Thread thread = new Thread(new ThreadStart(CustomTaskAsync));
thread.Start();
public void CustomTaskAsync()
{
..................
}
Method 3
Thread thread = new Thread(CustomTaskAsync);
thread.Start();
public void CustomTaskAsync()
{
...............
}
Using Task
Method 1
bool result ;
Task<bool> task = Task.Run<bool>(async () => await CustomTaskAsync());
str = task.Result;
public async Task<bool> CustomTaskAsync()
{
bool result;
result= await Task.Factory.StartNew(() => CustomTask());
return result;
}
private bool CustomTask()
{
bool result;
return result;
}
Method 2
CustomTaskAsync().Wait();
public async Task CustomTaskAsync()
{
await Task.Run(() => {
.................
});
}