1

I am a beginner and was hoping someone could tell me if i am using the correct way to use a if/else while using threads.

On my form the user has a checkbox which will determine which set of apps that will be installed. If the users select wireless enabled it installs the odyssey client.

please don't eat me alive i am just starting out.

So question's being:

  1. on the if/else statement is that best way?

  2. How can i Invoke the check box control? I understood how to make the progressbar work by invoking but confused about the check box.

    1. Is this best way to hook up progressbar considering i am dealing with wceload>? Or is it possible to hook it up with out pre set values. I.E 25; 35 %?

thankyou

namespace Program1 { public partial class Form1 : Form {

    private Thread myThread;

    delegate void UpdateProgressDelegate();



    private bool workerThreadDone;


    public Form1()
    {
        InitializeComponent();
    }


    public string Message = "";
    private void Form1_Load(object sender, EventArgs e)
    {



    }

    delegate void UpdateStatusInvoker(string statusText);

    private void MyWorkerThread()
    {



        while (!workerThreadDone)
        {

            Thread.Sleep(1000);

            using (RegistryKey Key = Registry.LocalMachine.OpenSubKey(@"Software\Apps\Microsoft .NET CF 3.5"))
                if ((Key != null && checkBox2.Checked))
                {
                    MessageBox.Show("Framework Exist, Wireless apps Enabled");
                    appcenter();
                    oac();



                }

                else
                    if ((Key != null && !checkBox2.Checked))
                    {
                        MessageBox.Show("Framework exist and NOT installing wireless apps.");


                        appcenter();

                    }


                    else
                        if (checkBox2.Checked)
                        {
                            MessageBox.Show("Framework not found,Installing with wireless");

                            NETCFv351();
                            NETCFv352();
                            sqlce1();
                            sqlce2();
                            sqlce3();
                            appcenter();
                            oac();


                        }

                        else
                            if (!checkBox2.Checked)
                            {
                                MessageBox.Show("Installing everything with NO wireless");
                                NETCFv351();
                                NETCFv352();
                                sqlce1();
                                sqlce2();
                                sqlce3();
                                appcenter();


                            }

        }

    }

    public void skipframework_progress()
    {
        this.statusBar1.Text = ("Framework Found!, Skipping");
        this.progressBar1.Value = 35;
    }
    // NETCFv35.Messages.EN.wm.cab
    public void NETCFv351()
    {
        this.progressBar1.Invoke(new UpdateProgressDelegate(framework_progress));

        Thread.Sleep(1000);

        string cab = "\\Program Files\\cabs\\NETCFv351.cab";

        string parm = @"/delete 1 """ + cab + @""" /silent";

        ProcessStartInfo psi = new ProcessStartInfo(@"wceload.exe", parm);
        Process p = new Process();

        p.StartInfo = psi;

        p.Start();

        p.WaitForExit();

    }
    public void framework_progress()
    {
        this.statusBar1.Text = ("Installing NET3.5 Framework");
        this.progressBar1.Value = 10;
    }

    //NETCFv35.Messages.EN.wm.cab
    public void NETCFv352()
    {
        string cab = "\\Program Files\\cabs\\NETCFv352.cab";

        string parm = @"/delete 1 """ + cab + @""" /silent";

        ProcessStartInfo psi = new ProcessStartInfo(@"wceload.exe", parm);
        Process p = new Process();

        p.StartInfo = psi;

        p.Start();

        p.WaitForExit();
    }
    //sqlce.ppc.wce5.armv4i.CAB
    public void sqlce1()
    {
        string cab = "\\Program Files\\cabs\\sqlce1.cab";

        string parm = @"/delete 1 """ + cab + @""" /silent";

        ProcessStartInfo psi = new ProcessStartInfo(@"wceload.exe", parm);
        Process p = new Process();

        p.StartInfo = psi;

        p.Start();

        p.WaitForExit();

    }
    //sqlce.repl.ppc.wce5.armv4i.CAB
    public void sqlce2()
    {

        string cab = "\\Program Files\\cabs\\sqlce2.cab";

        string parm = @"/delete 1 """ + cab + @""" /silent";

        ProcessStartInfo psi = new ProcessStartInfo(@"wceload.exe", parm);
        Process p = new Process();

        p.StartInfo = psi;

        p.Start();

        p.WaitForExit();
    }

    public void sqlce3()
    {
        //sqlce.dev.ENU.ppc.wce5.armv4i.CAB
        string cab = "\\Program Files\\cabs\\sqlce3.cab";

        string parm = @"/delete 1 """ + cab + @""" /silent";

        ProcessStartInfo psi = new ProcessStartInfo(@"wceload.exe", parm);
        Process p = new Process();

        p.StartInfo = psi;

        p.Start();

        p.WaitForExit();

        Thread.Sleep(1000);

        this.progressBar1.Invoke(new UpdateProgressDelegate(framework_done_progress));
    }


    public void framework_done_progress()
    {
        this.statusBar1.Text = ("NET3.5 Framework Installed");
        this.progressBar1.Value = 30;
    }





    public void appcenter()
    {

        string cab = "\\Program Files\\cabs\\appcenter.cab";

        string parm = @"/delete 1 """ + cab + @""" /silent";


        Thread.Sleep(700);

        ProcessStartInfo psi1 = new ProcessStartInfo(@"wceload.exe", parm);
        Process p = new Process();

        p.StartInfo = psi1;

        p.Start();

        p.WaitForExit();
        Thread.Sleep(700);
        this.progressBar1.Invoke(new UpdateProgressDelegate(appcenter_progress));
    }

    public void appcenter_progress()
    {
        this.statusBar1.Text = ("Appcenter Installed");
        this.progressBar1.Value = 35;
    }


    public void oac()
    {

        string cab = "\\Program Files\\cabs\\oac.cab";

        string parm = @"/delete 1 """ + cab + @""" /silent";

        ProcessStartInfo psi1 = new ProcessStartInfo(@"wceload.exe", parm);
        Process p = new Process();

        p.StartInfo = psi1;

        p.Start();

        p.WaitForExit();

        Thread.Sleep(700);

        this.progressBar1.Invoke(new UpdateProgressDelegate(oac_progress));
    }

    public void oac_progress()
    {
        this.statusBar1.Text = ("Odyssey Installed");
        this.progressBar1.Value = 65;
    }



    private void button1_Click_1(object sender, EventArgs e)
    {

        workerThreadDone = false;
        myThread = new Thread(MyWorkerThread);
        myThread.IsBackground = true;
        myThread.Start();


    }
}

}

4

1 回答 1

0

我已经重新编写了代码......看看它现在是否更有意义:

public partial class Form1 : Form
{

    private Thread myThread = null;

    public Form1()
    {
        InitializeComponent();
    }

    public delegate void ProgressDelegate(string message, int percent);
    public void DisplayProgess(string message, int percent)
    {
        if (this.InvokeRequired)
        {
            this.Invoke(new ProgressDelegate(DisplayProgess), new Object[] { message, percent });
        }
        else
        {
            this.statusBar1.Text = message;
            this.progressBar1.Value = percent;
        }
    }

    public delegate bool IsCheckedDelegate(CheckBox cb);
    public bool IsChecked(CheckBox cb)
    {
        if (cb.InvokeRequired)
        {
            return (bool)cb.Invoke(new IsCheckedDelegate(IsChecked), new Object[] { cb });
        }
        else
        {
            return cb.Checked;
        }
    }

    private void button1_Click_1(object sender, EventArgs e)
    {
        if (myThread == null)
        {
            button1.Enabled = false;
            myThread = new Thread(MyWorkerThread);
            myThread.IsBackground = true;
            myThread.Start();
        }
    }

    private void MyWorkerThread()
    {
        using (RegistryKey Key = Registry.LocalMachine.OpenSubKey(@"Software\Apps\Microsoft .NET CF 3.5"))
        {
            if (Key != null)
            {
                if (this.IsChecked(checkBox2))
                {
                    MessageBox.Show("Framework Exist, Wireless apps Enabled");
                    appcenter();
                    oac();
                }
                else
                {
                    MessageBox.Show("Framework exist and NOT installing wireless apps.");
                    appcenter();
                }
            }
            else
            {
                if (this.IsChecked(checkBox2))
                {
                    MessageBox.Show("Framework not found,Installing with wireless");
                    NETCFv351();
                    NETCFv352();
                    sqlce1();
                    sqlce2();
                    sqlce3();
                    appcenter();
                    oac();
                }
                else
                {
                    MessageBox.Show("Installing everything with NO wireless");
                    NETCFv351();
                    NETCFv352();
                    sqlce1();
                    sqlce2();
                    sqlce3();
                    appcenter();
                }
            }
        }
    }

    public void RunCabAndWait(string CabToRun)
    {
        string cab = "\\Program Files\\cabs\\" + CabToRun;
        string parm = @"/delete 1 """ + cab + @""" /silent";
        Process.Start(new ProcessStartInfo(@"wceload.exe", parm)).WaitForExit();
    }

    // NETCFv35.Messages.EN.wm.cab
    public void NETCFv351()
    {
        this.DisplayProgess("Installing NET3.5 Framework", 10);
        Thread.Sleep(1000);
        this.RunCabAndWait("NETCFv351.cab");
    }

    //NETCFv35.Messages.EN.wm.cab
    public void NETCFv352()
    {
        this.RunCabAndWait("NETCFv352.cab");
    }

    //sqlce.ppc.wce5.armv4i.CAB
    public void sqlce1()
    {
        this.RunCabAndWait("sqlce1.cab");
    }

    //sqlce.repl.ppc.wce5.armv4i.CAB
    public void sqlce2()
    {
        this.RunCabAndWait("sqlce2.cab");
    }

    //sqlce.dev.ENU.ppc.wce5.armv4i.CAB
    public void sqlce3()
    {
        this.RunCabAndWait("sqlce3.cab");
        Thread.Sleep(1000);
        this.DisplayProgess("NET3.5 Framework Installed", 30);
    }

    public void appcenter()
    {
        Thread.Sleep(700);
        this.RunCabAndWait("appcenter.cab");
        Thread.Sleep(700);
        this.DisplayProgess("Appcenter Installed", 35);
    }

    public void oac()
    {
        this.RunCabAndWait("oac.cab");
        Thread.Sleep(700);
        this.DisplayProgess("Odyssey Installed", 65);
    }

}
于 2013-04-29T21:09:15.017 回答