Oleg Sh
Oleg Sh

Reputation: 9013

code is executed long time when app starts first time

I have an WPF application, which has the following code:

    public static BitmapSource ToBitmapSource()
    {
        using (var screenBmp = new Bitmap(Convert.ToInt32(System.Windows.SystemParameters.PrimaryScreenWidth), 
            Convert.ToInt32(System.Windows.SystemParameters.PrimaryScreenHeight), 
            System.Drawing.Imaging.PixelFormat.Format32bppArgb))
        {
            using (var bmpGraphics = Graphics.FromImage(screenBmp))
            {
                bmpGraphics.CopyFromScreen(0, 0, 0,
                    0, screenBmp.Size);
                return Imaging.CreateBitmapSourceFromHBitmap(screenBmp.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
            }
        }
    }

and some code below. Its code executes when user selects the feature in app. Problem is this code executes long time, but only when app starts first time on this PC. If user deinstall and install again this app - it works quickly. Questions:

  1. How to "rollback" a system to check this problem again and again (don't want to reinstall Windows for one start)
  2. The best way to debug (place, which really do program slowly)
  3. How to fix it :)

Thanks

Upvotes: 1

Views: 103

Answers (3)

E.S.
E.S.

Reputation: 91

First of all, use a profiler (SlimTune, for example) to find out the boutleneck.

Upvotes: 0

user734028
user734028

Reputation: 1111

i dont work in wpf, but try doing the time consuming work in a separate thread, that might solve your problem.

Upvotes: 0

Ming Slogar
Ming Slogar

Reputation: 2357

  1. You probably can't rollback Windows. A better alternative would be to use VMware or another virtual machine software, and just keep recopying the disk file.
  2. If you start up a process which uses a lot of RAM and CPU (something like Prime95) you will be able to really slow down your machine.
  3. You are basically loading a bunch of separate DLLs in this function. To make it appear faster, create and run this function on a background thread. This will initialize all the classes. Then, when the user needs it, all of the DLLs will already be loaded into memory, making the function feel much faster.

Upvotes: 1

Related Questions