WPF App, open window very slowly for the first time - c#

I've a WPF App. Windows Start-up, run it. after 'OnStartup' ,the MainWindow appears,then It's very slow to open A window. Opening B Window is fast. Open B before A, Opening B window is slow.
Why is there such a situation and how to solve it?
It's slow because Window Show Method,It takes about 5s,second, 0.5s.
This only happens when Windows is first started.
If i don't restart Windows, just restart the App, just 1s+ .

I had the same problem using a Method from another class inside my Start-Up.
My Solution was just to change the method to a static one.
This works, cause the StartUp wasnt forced to create a new Object of the Class( there was a lot of Stuff going on in the ctor).

Related

Force to close current window first in WPF

I'm working WPF application, Currently it has one window and at specific time (As per my requirement). It's displayed on top most priority.
this.Visibility = Visibility.Visible;
this.Topmost = true;
Now, I want like user should close first this windows(WPF application window), till cannot access any other thing from system.(user cannot able access even other application also) Seems like to force close first this window
I've been searched, but not getting anything.
How Can i do that?
You can't.
Imagine another application tried to do the same thing at the same time - you can't close window A before window B, but you can't close window B before window A, but...
Edit: the short version above can be misunderstood, so in more detail:
Let's say, your application has this very important message for the user, so it opens a special window. The special property of this window is that as long as it exists, the user can't access any other window. This also means, the user cannot close any other window.
Now while the user reads the message, another application has an important message, so it opens a window, using the same method as you to block access to other windows until it is closed.
The user cannot close the second window, because the first window is special and must be closed first. The user cannot close the first window, because the second window is special and must be closed first => the UI is completely locked. The poor user must use Task Manager to kill one of the two applications or reboot the system.
See also Raymond Chen's more detailed discussion of this principle.

Opening WPF Window from another Window

I am have two different WPF applications running differently. now my requirement is to merge these application. Now Both application are WPF application. So I need to call Second application from the First Application So bascially I have created Two windows and I am initializing the Window2 from Window1 on a button click. Now the problem assuming that I have used the same resource files for both application.
What will happen if I close my First WPF application will the Second application will also get closed?
What should be the best approach in these situations?
If my main application window closes then the resource will also go out of scope and how should i ensure my appliction works correctly?
At the Application level you can set the ShutdownMode property to define the condition that will make the application terminate.
If the second application cannot run without the first one for some reason, as you suggest in your post, you could set this property to OnMainWindowClose. This way, if the first one is closed, the full application will stop
Spawn a new process on button click, that opens AppB. But that will keep both apps separate from each other, so no data sharing either. If you want to share data and code, add each page from AppB manually to AppA just like adding a third party control.

Intercept Opening Window - C#

Is there some way to watch for / intercept a window opening in Windows and then to block said window from opening? This would be for an application that's already running and I wouldn't want to stop the process, just close the popup window it spawns.
Update:
A process is already running (e.g. it's in Task Manager) but has no visible windows.
At some random time, that process will popup a window
I manually close the window (click "OK").
The process continues to run at this point and will, again, popup a window a bit later (repeating these steps).
I want to automate step #3, where I have to manually close the popup, by intercepting that window opening and closing it or hiding it or never letting it open in the first place. I don't want to do this by polling the open windows. I want to receive some event that a window is about to open.
I do not control this other application, so I can't otherwise change it. And I don't want to kill the process, itself.
If you're trying to do what I asked (allow only one instance of a program to run at a time), here is an elegant solution using a Mutex. You can probably copy and paste most of that code to achieve what you want.

Can we and how to open a new window and closes the previous window in the same thread?

I am asking this, because i want to know if when we are running an app, for start if we have an window to authenticate like a Log In window, after validating the user, can we open the Main Window in the same Thread without creating a new one?
I am trying to do this in WPF, but i think that is same thing in WPF or in Windows Forms.
Yes, you can.
Just do it.
When you generate a Windows Forms application via the IDE, it will generate the code for one form, as well as a Main function that displays the form at runtime. You can rewrite the Main method so it displays one form modally then displays the next form.
But there's a simpler way to achieve your objectives:
Have two windows: your Main window, where most of the work is done, and the login screen.
In the OnLoad event of your main window, create an instance of your login window and call ShowModal() on this instance.
If the login fails, then exit the application.
This question does not offer enough context to tell you how to do this in your specific case. In general you can just Close() a window, construct a new one and call Show() on it.
You should make sure the Application.ShutdownMode does not kill off your application when the window is closed though.

How can I speed up the opening of a WPF window in a VS add-in?

I have a Visual Studio add-in which opens a modal WPF window.
My problem is that the first time round, it takes 4 seconds for the window to appear which is a clear disservice to the client. So I'm wondering if there is a way to optimize this away?
Is there some kind of nifty code to preload the PresentationFramework (or whatever is slowing the thing down) when the add-in starts, rather than when it is actually used?
You may want to check your output window in VS to see if the pause is actually from loading DLL's that it didn't have already loaded. If that's the case, then you can try this:
When the application starts, load a blank hidden WPF window and close it.
This should "pre-load" the presentation framework (if that's actually the problem - its sometimes hard to tell with these cases.), so that when you call the needed window its ready to open.
Not the best solution, but users can usually wait 4 seconds in the first place.

Categories

Resources