c# WPF how to delay autostart of an application? - c#

My question is how can i delay the autostart on user logon in an WPF Project. Because its necessary on this application.
I tried:
void OnStartup(StartupEventArgs e)
{
Thread.Sleep(60000);
base.OnStartup(e);
}
but it seems like the OnStartup method isnt aviable. Its always an error. So there is something i dont get.

You can always just use
public App()
{
System.Threading.Thread.Sleep(10000)
}
on the App.xaml.cs
Of as already mentioned you need to subscribe to the OnStartup event on xaml.

This answer might have what you're looking for:
I think what you really want to do is to subscribe to the Startup
event. You can do this in your XAML file
Click here to get started with WPF

Related

WPF: Unable to raise Window.Closing event from app module

I take charge of a development of an older WPF modular application using Prism Library for WPF. In this case, entry point to the application for me is an overriden Initialize() method since I have no access to the Application.MainWindow's App class. This class along with some other helper classes is compiled to EXE file and DLL's.
Currently I'm facing to a problem that I have to catch the Window.Closing event which is not raised during closing the application. Normally this piece of code which is put into the constructor (in this specific case into Initialize() method) is working as expected
Application.Current.MainWindow.Closing += (s, e) =>
{
e.Cancel = true;
};
On the other hand, event Window.Closed is fired up without any issues.
In my opinion it's not possible to associate this event handler outside of Application.MainWindow's constructor, or do I something wrong? Please help me.
The issue was located. The OnClosing method which raises the Closing event is implemented in the shell and it looks like this
protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
{
e.Cancel = true;
ViewModel.StartShutdownSequence();
}
The StartShutdownSequence() encapsulates some logic regarding handling individual modules and at the end it calls Application.Current.Shutdown() which definitely shutdown the application.

App_Startup() VS OnStartup() when launching a WPF application

I am starting a new WPF project and I need lighting on the launch of the application.
I watched 2 different WPF projects to see how to begin.
The first one starts with the void App_Startup(object sender, StartupEventArgs e) method in the App.xaml.cs file, called directly from the App.xaml file (Startup="App_Startup").
The other one starts with the protected override void OnStartup(StartupEventArgs e) method in the App.xaml.cs file.
In addition of that, when I started my WPF project, the default code template starts in a third way with StartupUri="MainWindow.xaml" in the App.xaml file.
What are the differences between this three manners?
Startup is an event that is raised by the OnStartup method of the Application base class as you can see in the reference source. This is how it's implemented:
protected virtual void OnStartup(StartupEventArgs e)
{
VerifyAccess();
StartupEventHandler handler = (StartupEventHandler)Events[EVENT_STARTUP];
if (handler != null)
{
handler(this, e);
}
}
So if you override OnStartup in your App class and call base.OnStartup(e), the event will be raised and any attached event handler will be invoked. Whether you implement your logic in the event handler or directly in the overridden OnStartup is a matter of personal or application specific preference. There are no recommendations on why one should be better than the other.
When it comes to the StartupUri, it only makes sense to set this when you simply want to display a default window immediately on startup and you don't have any custom initialization logic.
In enterprise apps, it's common to have some kind of bootstrapper that sets up the application and its dependencies and create and show the main window programmatically rather than using the StartupUri property.
Startup="App_Startup" is just event handler for your application, which is started in App.xaml. It has event StartUp which you can subscribe to. If you want to have some "pre-loading" logic, this is perfect place.
If you want to have just your window shown, it is enough to specify URI (in StartUpUri) of that window, so application knows which file to load and what window to show :)
OnStartup raises event of application start (in base.OnStratUp), from the documentation:
OnStartup raises the Startup event.
A type that derives from Application may override OnStartup. The overridden method must call OnStartup in the base class if the Startup event needs to be raised.
There also can be put some pre-loading logic.
There is none recommended way, it is up to you and what you need.

UWP c# app with two kinds of onActivated listeners available - Activation by notification

Currently in my apps i am registering an onActivated listener to be executed and checked for my sharing activity like so:
Window.Current.Activated += Current_Activated;
with the method as follows to check for the activation state:
private async void Current_Activated(object sender, Windows.UI.Core.WindowActivatedEventArgs e)
{
if (e.WindowActivationState == CoreWindowActivationState.Deactivated)
{
}
else
{} }
Now i would like to implement an activation from a notification from a background task. The click on the notification is currently opening the app or brings it to the foreground if already open. This is standard behaviour. Now I would like to execute some code on activation by the notification but my method never gets called and I am not completely understanding Microsofts online material as it seems relatively simple.
I am registering the listener as such:
(Application.Current as App).Activated = new EventHandler<IActivatedEventArgs>(App_Activated);
and the method that never gets called looks like this:
private void App_Activated(object sender, IActivatedEventArgs e)
{ }
Are those two conflicting each other? is there something I can do to implement the notification activation into my existing method?
I've read through this and many more pages: https://blogs.msdn.microsoft.com/tiles_and_toasts/2015/07/08/quickstart-sending-a-local-toast-notification-and-handling-activations-from-it-windows-10/
Even from a test app i downloaded i don't really understand what needs to be done. Besides that i don't need to pass any info from the notification to the main activity. Simply just open it and execute one command of refreshing the page, but only on notification activation.
You should just override the OnActivated(IActivatedEventArgs args) method in your App.xaml.cs and check whether the args.Kind property is ActivationKind.ToastNotification.
You can find more info about handling an UWP app activation here.
As Marian said, you need to override the OnActivated method in your App.xaml.cs class. The OnActivated method is called when your toast is clicked, and it contains the toast arguments, so that you know what toast was clicked. Note that you must perform the same Frame initialization in your OnActivated as you do your OnLaunched, since if the user clicks on a toast while your app is closed, only the OnActivated method will be called - the OnLaunched won't be called.
This Quickstart explains exactly how to handle toast activation, and includes a full code sample: Quickstart: Sending a local Toast notification

How to delete file, then user closes program in WPF?

I know full path of file var temp_file = Path.Combine(_directoryName1, "temp.ini"); which need to be deleted in the end of program working. How could I do this ? As I know it is possible to realize via OncloseEvent(). In addition, I dont know exatly how user will close application via alt+f4 or via buttons.
So far, I have tried to use this code below from almost the same question How to override onclose event on WPF?
protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
{
//do my stuff before closing
base.OnClosing(e);
}
And I have added it in App.xaml.cs but it doesn't work. VS2013 says that he don't know such method base.OnClosing(e);
Is there any mistake or another way out?
Window.Closing is for a specific Window. That's probably not what you want in any case, because the Closing event can be cancelled. Window.Closed is likely a better choice.
To run something when the program is closed -- not tying yourself to a window -- you should subscribe to the Application.Exit event instead.

How can I store the data of a windows phone when the user press Start button?

Something is wrong with my program, while is using the application and when exit and resume, the application crashes..
I'm using a List variable but, when the application resume, it has nothing in it.
I was using this.
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
if (State.ContainsKey("c"))
{
App.Contenedor.Add((List<int>)State["c"]);
}
}
protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
{
State["c"] =(List<int>)App.Contenedor[0];
}
It seems working, but I'm not sure.. what do you think people?
You need to read up on Tombstoning, the process where application sleeps while not active.
Check out the Activated and Deactivated events.
You are saving/restoring data in the wrong methods. You shoud be saving data in OnNavigatedFrom and restoring data in OnNavigatedTo.
The Tombstoning is rarely invoked in Mango anymore - your application sort of exists in limbo while you are using other apps on the phone.
All the events that have to do with saving and restoring state are in App.xaml.cs - respond to them if necessary.

Categories