About status: use dialog saying Loading.

About Not responding: use threads

Sent from my HTC

-----Původní zpráva-----
Od: Booter <[email protected]>
Odesláno: 23. srpna 2010 20:22
Komu: DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML
Web Services,.NET Remoting <[email protected]>
Předmět: [DotNetDevelopment] Simple Help


Hello All,

I have been tinkering around with some C# applications that I wrote
over the summer for my job.  The one that I have a question about is a
simple app that connects to network printers.  The user chooses which
lab printers they want to map and it will map them.  I was trying to
incorporate a loading bar so that the user doesn't sit there and keep
clicking my "Map Printers" button since right now it seems like the
program just stalls.

Now my problem is that when run the program goes into a "This program
is not responding" phase but will still map the printers so to the end
user it appears that the application just crashed and didn't map their
printers.

Any help/ suggestions to make my app better would be greatly
appriciated.

Booter


*****************************CODE**************************

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace WindowsFormsApplication1
{

    public partial class Form1 : Form
    {
        // Set up AV Print array
        String[] avArray = new string[] {"\\\\sith\\orion","\\\\sith\
\color_orion","\\\\sith\\sun_av_classroom"};

        // Set up Anderson Print Array
        String[] anderArray = new string[] {"\\\\sith\\Anderson1","\\\
\sith\\color_anderson"};

        // Set up Cafe Print array
        String[] cafeArray = new string[] {"\\\\sith\\cafe","\\\\sith\
\color_cafe"};

        // Set up ERC Classroom Print Array
        String[] ercClassArray = new string[] {"\\\\sith\\erc","\\\
\sith\\color_erc"};

        // Set up ERC Design Print array
        String[] ercDesignArray = new string[] {"\\\\sith\
\erc_studio","\\\\sith\\erc","\\\\sith\\color_erc"};

        // Set up GIS Print Array
        String[] gisArray = new string[] {"\\\\sith\\gis","\\\\sith\
\color_gis"};

        // Set up Merc Print array
        String[] mercArray = new string[] {"\\\\sith\\merc","\\\\sith\
\color_merc"};

        // Set up Mag Print Array
        String[] magArray1 = new string[] {"\\\\sith\\magellan1","\\\
\sith\\color_magellan","\\\\sith\\magellan2","\\\\sith\\big_plotter","\
\\\sith\\little_plotter"};

        // Set up Mag Print Array
        String[] magArray2 = new string[] { "\\\\sith\\magellan2", "\\\
\sith\\color_magellan", "\\\\sith\\magellan1", "\\\\sith\
\big_plotter", "\\\\sith\\little_plotter" };

        // Set up Titan Print array
        String[] titanArray = new string[] {"\\\\sith\\titan","\\\\sith
\\magellan1","\\\\sith\\color_magellan","\\\\sith\\magellan2","\\\\sith
\\big_plotter","\\\\sith\\little_plotter"};

        // Set up Viking Print Array
        String[] vikingArray = new string[] { "\\\\sith\\Viking", "\\\
\sith\\magellan1", "\\\\sith\\color_magellan", "\\\\sith\\magellan2",
"\\\\sith\\big_plotter", "\\\\sith\\little_plotter" };

        [DllImport("winspool.drv")]
        public static extern bool AddPrinterConnection(string pName);

        List<string> mappedPrinters = new List<string>();
        List<string> notMappedPrinters = new List<string>();


        int totalChosen;
        int totalMaped = 0;

        public Form1()
        {
            InitializeComponent();
        }

        public void mapPrinters()
        {
            if(AV.Checked)
            {
                //map AV
                foreach(string c in avArray)
                {
                    label1.Text = "Attempting to map " + c;
                    if (AddPrinterConnection(c))
                    {

                        mappedPrinters.Add(c);
                    }
                    else
                    {

                        notMappedPrinters.Add(c);
                    }
                    progressBar1.Value++;
                }
            }
            if(Ander.Checked)
            {
                //map ander
                 foreach(string c in anderArray)
                {
                    label1.Text = "Attempting to map " + c;
                    if (AddPrinterConnection(c))
                    {

                        mappedPrinters.Add(c);
                    }
                    else
                    {

                        notMappedPrinters.Add(c);
                    }
                    progressBar1.Value++;
                }
            }
            if(ERC_Class.Checked)
            {
                //map ercClass
                 foreach(string c in ercClassArray)
                    if (AddPrinterConnection(c))
                    {

                        mappedPrinters.Add(c);
                    }
                    else
                    {

                        notMappedPrinters.Add(c);
                    }
                    progressBar1.Value++;
            }
            if(ERC_Design.Checked)
            {
                //map ercDesign
                 foreach(string c in ercDesignArray)
                {
                    label1.Text = "Attempting to map " + c;
                    if (AddPrinterConnection(c))
                    {

                        mappedPrinters.Add(c);
                    }
                    else
                    {

                        notMappedPrinters.Add(c);
                    }
                    progressBar1.Value++;
                }
            }
            if(GIS.Checked)
            {
                //map gis
                 foreach(string c in gisArray)
                {
                    label1.Text = "Attempting to map " + c;
                    if (AddPrinterConnection(c))
                    {

                        mappedPrinters.Add(c);
                    }
                    else
                    {

                        notMappedPrinters.Add(c);
                    }
                    progressBar1.Value++;
                }
            }
            if(I_Cafe.Checked)
            {
                //map cafe
                 foreach(string c in cafeArray)
                {
                    label1.Text = "Attempting to map " + c;
                    if (AddPrinterConnection(c))
                    {

                        mappedPrinters.Add(c);
                    }
                    else
                    {

                        notMappedPrinters.Add(c);
                    }
                    progressBar1.Value++;
                }
            }
//             if(IRC.Checked)
//            {
//               //map IRC PRINTER
//                if (AddPrinterConnection("129.82.106.197"))
//                    MessageBox.Show("Printer at the IRC Lab is
mapped");
//                else
//                    MessageBox.Show("Printer at the IRC Lab was not
mapped");
//
//           }
            if(Mag.Checked)
            {
                //map mag
                foreach (string c in magArray1)
                {
                    label1.Text = "Attempting to map " + c;
                    if (AddPrinterConnection(c))
                    {

                        mappedPrinters.Add(c);
                    }
                    else
                    {

                        notMappedPrinters.Add(c);
                    }
                    progressBar1.Value++;
                }
            }
            if(MERC.Checked)
            {
                //map merc
                 foreach(string c in mercArray)
                {
                    label1.Text = "Attempting to map " + c;
                    if (AddPrinterConnection(c))
                    {

                        mappedPrinters.Add(c);
                    }
                    else
                    {

                        notMappedPrinters.Add(c);
                    }
                    progressBar1.Value++;
                }
            }
            if(titan.Checked)
            {
                //map titan
                 foreach(string c in titanArray)
                {
                    label1.Text = "Attempting to map " + c;
                      if (AddPrinterConnection(c))
                    {

                        mappedPrinters.Add(c);
                    }
                    else
                    {

                        notMappedPrinters.Add(c);
                    }
                    progressBar1.Value++;
                }
            }
            if(viking.Checked)
            {
                //map viking
                 foreach(string c in vikingArray)
                {
                    label1.Text = "Attempting to map " + c;
                        if (AddPrinterConnection(c))
                    {

                        mappedPrinters.Add(c);
                    }
                    else
                    {

                        notMappedPrinters.Add(c);
                    }
                    progressBar1.Value++;
                }
            }

            if (notMappedPrinters.Count != 0)
            {
                string prompt = "Printers not mapped: \n";
                foreach (string c in notMappedPrinters)
                {
                    prompt = prompt + c + "\n";
                }
                MessageBox.Show(prompt);
            }
            if (mappedPrinters.Count != 0)
            {
                string prompt = "Printers mapped: \n";
                foreach (string c in mappedPrinters)
                {
                    prompt = prompt + c + "\n";
                }
                MessageBox.Show(prompt);
            }

        }

        private void map_printers_Click(object sender, EventArgs e)
        {
            totalChosen = sum();
            progressBar1.Maximum = totalChosen;
            label1.Text = "Please wait.  Initialzing program";
            mapPrinters();



        }

        private void Exit_app_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private int sum()
        {
            int sum = 0;


            if (AV.Checked)
                sum = sum + avArray.Length;
            if (Ander.Checked)
                sum = sum + anderArray.Length;
            if (ERC_Class.Checked)
                sum = sum + ercClassArray.Length;
            if (ERC_Design.Checked)
                sum = sum + ercDesignArray.Length;
            if (GIS.Checked)
                sum = sum + gisArray.Length;
            if (I_Cafe.Checked)
                sum = sum + cafeArray.Length;
            //             if(IRC.Checked)
            //            {
            //               //map IRC PRINTER
            //                if
(AddPrinterConnection("129.82.106.197"))
            //                    MessageBox.Show("Printer at the IRC
Lab is mapped");
            //                else
            //                    MessageBox.Show("Printer at the IRC
Lab was not mapped");
            //
            //           }
            if (Mag.Checked)
                sum = sum + magArray1.Length;
            if (MERC.Checked)
                sum = sum + mercArray.Length;
            if (titan.Checked)
                sum = sum + titanArray.Length;
            if (viking.Checked)
                sum = sum + vikingArray.Length;

            return sum;

        }
    }
}

Reply via email to