2013-04-29 47 views
1

我是一名初學者,希望有人能告訴我,如果我在使用線程時使用正確的方式來使用if/else。If/else invoking controls

在我的表單上,用戶有一個複選框,它將決定將要安裝的應用程序集合。如果用戶選擇啓用無線,則會安裝奧德賽客戶端。

請不要吃我活着我剛剛開始。

所以,問題的存在:

  1. 的if/else語句是最好的方法?

  2. 我該如何調用複選框控件?我明白如何通過調用而使進度條工作,但對複選框感到困惑。

    1. 這是最好的方式來掛鉤進度欄考慮我正在處理wceload>?或者有可能將它與預先設定的值掛鉤。 I.E 25; 35%?

三江源

命名空間PROGRAM1 { 公共部分Form1類:表格{

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(); 


    } 
} 

}

+0

'Thread.Sleep(0);'wat – tnw 2013-04-29 19:22:01

+0

另外,'我知道我在做這個錯誤',所以你遇到的具體問題是什麼?錯誤?意外的行爲? – tnw 2013-04-29 19:23:44

+0

修復了睡眠(0),我試圖在一個點上隱藏waitcursor。更改爲1000, – user2190928 2013-04-29 19:24:53

回答

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); 
    } 

} 
+0

這真的幫助我理解。非常感謝你 – user2190928 2013-04-30 02:02:44