0

I have a small child form (dialog) in my c# application that loads the available printers on the machine/network into a listbox in the Load event, and in the Shown event selects the one in the list that is the Default printer

Trouble is, even though there are only about 8 available printers there is still a noticable load time of a second or two, so I'm thinking that mycode is wrong.

here is my code (taken from another post on StackOverFlow about getting/setting the default printer):

using System;
using System.Drawing.Printing;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Collections.Generic;

    private void frmPrinters_Shown(object sender, EventArgs e)
    {
        PrinterSettings settings = new PrinterSettings();
        foreach (string printer in PrinterSettings.InstalledPrinters)
        {
            settings.PrinterName = printer;
            if (settings.IsDefaultPrinter)
            {
                this.lstAvailablePrinters.SelectedItem = printer;
            }
        }

    }

    private void frmPrinters_Load(object sender, EventArgs e)
    {
        List<string> lstOfPrinters = new List<string>();

        foreach (var item in PrinterSettings.InstalledPrinters)
        {
            this.lstAvailablePrinters.Items.Add(item.ToString());
        }
        //listAllPrinters();

    }

public static class myPrinters
{
    [DllImport("winspool.drv", CharSet = CharSet.Auto, SetLastError = true)]
    public static extern bool SetDefaultPrinter(string Name);
}

is there some different way to handle this, or better way, or what am I doing wrong?

thanks Philip

Our Man in Bananas
  • 5,809
  • 21
  • 91
  • 148
  • [This](http://msdn.microsoft.com/de-de/library/86faxx0d%28v=vs.110%29.aspx) shows the (simplified) order of event to be: Load, Activated, Shown. (Unless you mess it up - see [here](http://stackoverflow.com/questions/3070163/order-of-events-form-load-form-shown-and-form-activated-in-windows-forms).) So you should try to __move all the code__ to the `Shown` event. Also displaying a Waitcursor will give the user the secure feeling, that the machine is working for him or her. – TaW Jun 19 '14 at 17:00
  • 1
    @TaW `displaying a Waitcursor will give the user the secure feeling` - not really. blocking the UI thread for a number of seconds causes Windows to label your application as "Not Responding..". The right approach from a UX standpoint is to do stuff in a background thread and show a loading overlay over your UI, while disabling controls to prevent the user from re-firing the job, but keeping the UI thread free. – Federico Berasategui Jun 19 '14 at 17:21
  • 1) OP was talking a bout __one second or two__ 2) Not blocking the UI thread makes sense if the user can actually do anything, which here, obviously, is not the case 3) The WaitCursor is normal, a loading overlay is also OK. – TaW Jun 19 '14 at 17:42
  • @TaW I don't buy that for a second. Anything that can take time should be on a background thread. Two seconds on my machine is usually 10 on my clients'. – DonBoitnott Jun 19 '14 at 19:54
  • I never even considered a background worker, but of course, that's what they're for... will give that a loo. – Our Man in Bananas Jun 19 '14 at 20:56

1 Answers1

0

You probably can't do anything about the fact that retrieving InstalledPrinters or Calling SetDefaultPrinter is slow. However what you should do is execute any slow code asynchronously in order to avoid blocking the UI thread.

First you can detect which code is slow using the Stopwatch class.

Secondly, you can use a BackgroundWorker or something else to execute the slow code.

Sam Bauwens
  • 1,317
  • 11
  • 18