I have a complex WPF application that is using a lot of resources from the shared Resource Dictionary.
The first Window initialized takes 8 seconds to initialize. The performance issue is less on SSD disk drives but still it requires 2 seconds.
I tried to use the Visual Studio Profiler and it shows big expense of time on InitializeComponent();
of the windows that needs to be displayed.
I believe it is related to the Resource dictionary used but I can't replace it because I really need it and because all windows and WPF elements are using the StaticResource references.
I tried to optimize the launch as much as it is possible.
I created many background threads but this didn't helped too much. Whenever a window needs to be displayed it must be attached on UI thread under the same Dispatcher. This makes a big performance issue and all UI and any Progress bar left on the screen is blocked.
So to summarize. From the point when the ShowDialog is called until the window is displayed it
takes 8 seconds. This is visible only on the first window. Any other window opened after that is displayed quickly.
Now I am asking firstly what happens in the background and why this delay is so big and second what can be done to increase the startup speed.
I didn't mentioned but there are no Exceptions or DataErrors present during the launch so it is
not related to Exceptions.
I believe it is something with the initialization of Buttons and other components because almost all of them have the ControlTemplate restyled.
Lots of assemblies need to be loaded and lots of code must be JIT compiled before your first window can be shown. One useful technique to reduce startup time is to structure your code in such a way that types are not loaded before they are needed. It may be preferable to get a blank window up on screen with a wait indicator before delving into code outside of the core WPF assemblies. Optimize for that scenario.
Avoid loading images/media and other resources too early if you are trying to get something up on the screen as soon as possible.
Avoid loading any data synchronously, and do as little in your view and view model constructors as possible. Defer the loading of data until your view has been shown (throw up a wait indicator if necessary).
If you think your Xaml resources are a problem, split them up, and have each view pull in only the resources it needs. Don't merge them into App.xaml. You might also look into how to share the resources more efficiently across multiple views.
Throwing up a splash screen can improve the perceived startup time. Getting anything up on the screen to let the user know your app is actually doing something goes a long way.
Lastly, don't fret too much; poor startup time is the hallmark of WPF applications, and in the end, there's only so much you can do.
You can also use the ProfileOptimization class to improve startup time over subsequent sessions of the program. May not help you, the developer, but may have an impact on your users for the better.
Related
So this may be a noobish type of question but here is what I'm wondering.
Let's say I have two screens, the first screen is idk, Screen1 for example. Let's say the user hits OK on Screen1 which takes them to Screen2.
What I'm currently doing is closing the first screen ( Screen1 ) so that the screen isn't in the background because the user will no longer need that screen nor should they be able to go back to that screen.
My question are essentially is these:
Let's say that I didn't close that previous screen in the background, how will that effect the program?
Will it slow the program down, because that screen is still running or will it not effect the program at all?
I'm just thinking in terms of efficiency. Does it really matter if I don't Close(); that previous screen?
There is of course some amount of overhead in having a window open. It will receive various window messages from the operating system and process them. However, if the window is not really doing anything, then the overhead is insignificant. For example, you can open 20 copies of the Windows calculator and not see any noticeable degradation in your computer's performance because those windows are not really doing anything until you interact with them.
Basically, the difference between closing the window vs not is insignificant (unless the window is running animations or playing a video or something else causing it to change regularly).
So, you should probably be making the decision as to whether to leave the window open or not based on the desired user experience for your application. Since you stated that the user should never go back to that screen, then it probably makes sense to close it, but that is really up to the application designer (or whoever is thinking about the user experience for the application).
I'm debugging a problem to do with our custom splash screen and am wondering how I can simulate the longer loading time of a C# application on a cold boot.
I don't mean by adding a sleep events when the application loads up. I mean somehow adding a delay in the load up of .net or maybe creating a dll to reference that somehow I can hack to take far longer to load.
I appreciate this is an odd request, but is there anything I can do?
I'm doing some performance minded work on an existing winforms project.
This form is made dynamically rather than at design time, and loads a few dozen user controls, as well as binding them to a DataSet object with a small set of data.
Using some profiling tools (ANTS, Avicode), i can see most of time, around 50%, is taken by the code i have to show a tab page i have on the form. drilling down, i see it divides roughly in half to two operations:
1) a long line of VisibleChanged/SetVisibility/SetVisibleCore operations, all in the winforms namespaces. How can i know if the time it takes (4.5 seconds in my case) is sane? I do have a lot of user controls in my form.
2) A long line of RightToLeftChanged operations, again, around 4.5 seconds. Here too, I'm wondering if this is expected/sane performance of WinForms.
any advice regarding finding the performance issues here or whether the performance i'm getting is normal would go a long way to help. Thanks in advance :)
I wrote a small autohotkey script that removes the border, titlebar, and resize handles of a window, and centers it on the first monitor. This works for most applications and games, but some (bioshock 2, APB, etc) replace their window style instantly after removing it. Is there a way to block window style changes?
I would prefer to keep this in AHK, but the title has c# in it because I would like to convert my application to that down the road, and if it's only possible in c#/c++ then now would be a good time to start conversion.
I could be missing something, but I doubt this is going to be easy. The behavior you describe is one of an application which is either monitoring for changes in its window style, or just constantly redrawing them nonstop to prevent these changes. Of course you could just Loop through and fight against it where you remove, it replaces, you remove, it replaces, but that won't solve anything.
One way you could try is to create a .dll and inject it into the app's process, and then hook some API calls and simply return before anything gets redrawn. Google for 'detours hooking' for some examples. That might work, but would be out of the scope of AHK. And your simple 15 minute AHK script would turn into a much bigger project. =(
i have a C# application, and id like to be able to make a system, so that within the program, you can display the sourcecode of the application. easy enough so far right?
well, i need them to be able to edit that code, almost like the debug break option... then load the new code and continue without stopping the program. so my problem is, that when this app loads its several thousand lines of code, and it takes a good block of time. after its loaded, it needs to do several hundred operations before allowing user input the first time it loads. also, it has a tcp client in it, and it is very important that it does not get disconnected.
i want people to be able to edit the source, click a button, and wait a few seconds, and have the new code inserted and "rehashed" so to speak, without having to break the overall function of the application.
im looking thorough code examples where possible and an input weather this is possible or not
~ thanks
If you want to allow people to make arbitrary changes to your program, that would be very complex. However, if you want to let them change specific behavior (like rewriting a calculation algorithm) you could have a look at Microsoft.CSharp.CSharpCodeProvide as discussed here.
I don't think you can do that (change a .net app without rebuilding it) but you can have dynamic code loaded and run at any time..
Some people use plugins with Boo, people can change the plugins and these can be loaded at any time by the main app.
But I would suggest you have a look at the Ruby usage inside SilverLight..
This is something completely different, but its something I'm reading on how to start playing with Dynamic code handling: here