WPF Application starts up too slowly - c#

I have problems with my current application which is developed in C# with WPF. The application consists of different flowcharts each contained in its own separate window.
At start-up all flowchart classes are instantiated and initialized in their window. Then the windows are made invisible and the flowchart menu appears.
With each new flowchart the application start up gets slower. The window initialization seems to consume a lot of time.
How could I approach this problem?
I thought of initialization when first needed or background initialization.
Note: I forgot a very important fact: The flowchart menu is created based on the other flowcharts, since every single flowchart is rendered as an image in order to display a thumbnail menu button. This is the problem which brough me to the performance impact anyway.

You have many approaches to this problem.
First, you have to remove the initialization from the c'tor.
Where to move it?
Either to Window_onLoad event, this way the app will first loaded and then start initializing the flow charts (using a new thread will avoid UI freeze).
Or once the user selects the required flow chart from the menu, you show the window and start initializing the inner flow chart, display a nice "loading.." animation while busy, and show the flow chart once you done.
I'd prefer the second approach, init each object when it's needed.
Edit:
Since you have to render the thumbs based on the flowcharts, i would do the following:
Move the flowcharts init to the main window Loaded event, and init each flowchart on a different thread, while busy display a nice "please wait.." animation. This way the main app window appears, the user see that the app is loading, your thumbs will be created simultaneously therefore loading time will be reduced. Once all the thumbs created, hide the animation.

Don't instantiate all the flowchart classes before you load the main window.
As a first approach I'd go with initialisation when needed.
If this proves to be unpopular as people don't like waiting then go for a background initialisation with the most popular or most recent flowchart instantiated first. This will be right most of the time so the user won't have to wait.
If you need to have a thumbnail of the flowchart then why not save the thumbnail from when the flowchart was last rendered and use that? So, when the flowchart is created first saved save a thumbnail at that point. Then when you populate the list pull that out of the database/off disk and render that.

Nobody's mentioned profiling yet, but why not just try (the last paragraph of) this?
You will easily see the dominant reason for the long time being taken.
Chances are, it's something fairly trivial that you can easily fix.
If you want more of an explanation, look here.

Related

C# launch form when images fully loaded

I'm making C# application design with images, but when I launch it, I see loading images (~1sec), so how to make simple loader, when images (background, logo, etc..) fully loads, to show my app? I know it's possible but I don't know how to. Thanks so much!
This sounds like standard behaviour. The controls get painted one by one in z-order and if one is slow the rest may appear to flicker.
You could try double buffering the whole form as shown here:
How to fix the flickering in User controls
Alternatively you could suspend the layout while everything is drawn and resume it afterwards:
How do I suspend painting for a control and its children?

How to display something similar like the splash screen during a 10-30 sec load document process in a WPF application?

My WPF application has an import file function. Sometimes the process takes 1-2 sec but sometimes it is 20-30 sec. For this cases I would like to display something similar as the built in splash screen, and disable any UI interaction.
Although I know I can display a borderless modal window with a custom background image, before I reinvent the wheel I am asking if there is a better idea, or pre-written (open source) component for it.
I also have doubt how can I implement this delayed thing. I mean I do not want to show and disappear this window for 1-2 secs, just for cases that take longer. Well this is a separate topic I know but both questions belong to the question to be solved in the title.
Thanks in advance.
I would use the BusyIndicator control of the Extended WPF Toolkit. It disables the background so user can't use the application while it's displayed. You also have a DisplayAfter property to delay the visibility of the control.

How to change into different sections of your application easily

Im currently trying to create an application that will require 10+ different "pages" with different content and controls, and i need to switch back and forth between them on particular events.
What ive been doing, is just creating all the different sections in grids, and setting their visibility to collapsed, and then when i need to show them, just switch out the visible grid to the new one.
This has several drawbacks, im assuming its very poor from a coding standpoint, and this pretty much dis-allows me from using the designer at all. (i have no idea what performance implications it has, either)
on top of that, every time i switch to the new page, i need to reset all the components (textbox's etc) to their default states, as they dont get reset by becoming invisible :P
on to my question: i need a way to map out all the different pages, provide visually attractive transitions between them, and be able to use a designer to create them (and i dont mean designing it somewhere and then just copying the xaml)
I had looked around, and ran into SketchFlow and it seemed like the perfect solution, i could fade between pages and map everything on a flow chart easily, and then i realized it was only for app prototypes and i couldnt actually compile it as a normal application... and i needed to inherit from a custom Window class aswell.
is there something out there that allows me to do this? or how can i code this to work properly?
note: this ABSOLUTELY needs to stay within one window. i cant venture out into having 10+ different windows that pop up every time i need to change to something. as this happens very frequently
Split the separate sections in individual user controls. This would allow you to design each of them easily. Then on your form use code to create and load a new instance of particular user control that represents the section you need to show, and when transitioning, load the new section and unload the current. this would allow your form to stay relatively lightweight.
An alternative is to create a navigation application and split your sections into separate XAML view and use the standard navigation service to switch between them.
WPF Navigation Overview
Creating Navigation Applications video tutorial
You might wanna convert your "Pages" to usercontrols and use some transitions like mentioned in the below link to switch between controls
http://www.tanguay.info/web/index.php?pg=codeExamples&id=280
for more on using transitions look here
http://www.japf.fr/2009/04/adding-transitions-to-a-mvvm-based-dialog/
or
http://www.japf.fr/2008/07/8/comment-page-1/

Windows Forms advanced visual effects

I have an design idea about the appearence of a small program , which is basically a windows form with a combo and one button.What i would like to do is the following.When i click on the .exe of the program to start it , i would like to have an non-standart start up of the window.To be more specific i will give you an example - i click on the .exe , upon which some dots appear in a random matter all over the screen , after those points appear they start moving in a spiral way so finally they merge into the standart square windows form shape.So my question is - is there a free API or anything similar with the help of which its easily doable or there would be a lot of work needed from myself to create those API's ?
Thanks in advance
This will be extremely difficult to do.
It will also be very annoying for the end-users.
In short I see two options:
Render your form to a bitmap and render peices of the bitmap to a full-screen layered window. You'll have to call UpdateLayeredWindow repeatedly to get the animation working but that should be a good lead. I could see getting 10-20 fps with this method.
Take a screenshot of the desktop, create a full screen borderless topmost window, render the screenshot, then render your animation on top. This will prevent any other windows from recieving input while the animation is playing.
Either way your users will hate you.
As SLaks has already said, that will be pretty annoying for the end-users.
If you ask whether it is doable, I would say yes, everything is doable in programming, it all depends on the effort you're ready to put into.
As a very simple algorithm, here are some steps I would go through for your achievement:
Create a System.Windows.Forms.Form;
Set Form.ShowInTaskBar= false;
Set the Form.TransparencyKey property;
Set the Form.ControlBox= false;
Set Form.TopMost= true;
Drop a PictureBox control on your Form;
On the Form.Load event, take a screenshot of the current desktop and set it as the image of your PictureBox.
Then, build an animated GIF, and superpose it to your form.
You will most likely appreciate, I guess, the following link which discusses about C# Winforms Animation.
Disclaimer: This is an arbitrary algorithm off the top of my head. Besides, I illustrated the steps I would go through in order to achieve such objective, though I have never ever performed WindowsForms animation.
Althouth this might be cool to program, users are conservatives and "always anxious" about program startups, so, as already mentioned, this might become pretty annoying for the end-users.
I hope this helps you through anyway!

Prevent controls from being redrawn in C#?

I have a form that contains a lot of runtime generated controls (image, button, panel,...), about 100 controls (I'm making a card matching game so there is a lot of controls in the form).
I place the generating code into the constructor and each time the app starts, it took about 3-5s to load all the controls completely.
If I bring another window on top and then back to my app, the controls will be redrawn again.
How can I prevent the controls from being redrawn? If you don't mind, please give me a simple example in C#.
Any help is appreciated!
I found this article that explains how to do this in .NET by calling the WIN API SET_MESSAGE function to set the WM_SETREDRAW flag for the control you do not want updated. Although you can stop certain controls from updating, are you sure you can't approach this issue by reducing the number of controls on the page? 100 Controls seems like a lot and may be an indication that you need to have multiple views.
Enjoy!
My suggestion is to use the form as a drawing surface and draw your card bitmaps directly onto the form. Its not hard to do.
You can add a handler to the form Paint event which will give you parameters with a Graphics object. Use graphics.DrawImageUnscaled to draw each card at the location you want.
This will make the app much much faster.
Preventing a control from redrawing is fairly pointless. You'll get a hole where a control was supposed to appear, your user won't have any use for that hole.
It redraws slowly simply because you have too many controls. You can only get it to redraw faster by using less controls. Or by using controls that can display multiple items in one window, like ListBox, ListView, TreeView, DataGridView.
Note that your specific issue is fixed in Vista and Windows 7. The Aero theme uses off-screen buffering for windows. Which means that windows don't need to repaint themselves anymore when they are obscured by another window. You will however still get slow redraws when the user minimizes the window and restores it.
You might want to consider using a single data table control. A ListView (or something like ObjectListView) may be a good option.
If your data isn't really a list/table, you should split the controls into separate tab pages, which would improve both performance and usability.

Categories