2012-07-26 52 views
-1

下面是我有一個處理連接和串行設備交互的類的代碼。我從另一個類中調用connect函數,就像下面緊接着的那樣。我的問題是:更新我的進度條的最佳方式是什麼?它位於與初始化後臺工作者不同的類上。我遇到問題,因爲我正在嘗試更新跨線程和跨類的內容。將表單類作爲參數傳遞給我的連接函數,但後來我必須通過後臺工作人員和報表進度函數;它只是凌亂不潔。有一個更好的方法嗎?任何幫助表示感謝,並提前感謝。報告類和線程之間的後臺工作進程

類,我調用連接功能的(也是我的WPF類):

public partial class agentRadio : Window 
{ 
    private void connectButton_Click(object sender, RoutedEventArgs e) //Connect button clicked 
    { 
     agentradioread.connect((string)portsOpen.SelectedValue, this); 
    } 
} 

類來處理我的系列互動:

class agentRadioRead //Handles connection and reading of device values 
{ 
    /*Local variable declarations*/ 
    SerialPort agentSerial; 
    BackgroundWorker connectWorker; 

    string resultMessage = "Error: The connect function has completed without setting this status message properly."; 
    byte[] data = new byte[2246]; 

    /*Public methods*/ 

    public void connect(string selectedPort, agentRadio agentR) //Connects device, reads values, stores values, displays status message 
    { 
     agentSerial = new SerialPort(selectedPort, 9600, Parity.None, 8, StopBits.One); 
     connectWorker = new BackgroundWorker(); 
     //connectWorker.WorkerReportsProgress = true; 
     connectWorker.DoWork += new DoWorkEventHandler(initialRead); 
     //connectWorker.ProgressChanged += new ProgressChangedEventHandler(reportProgress); 

     connectWorker.RunWorkerAsync(agentR); 
    } 

    /*Private methods*/ 

    void initialRead(object sender, DoWorkEventArgs e) 
    { 
     agentSerial.Open(); 
     agentSerial.BaseStream.Flush(); 

     byte[] result = new byte[7]; 
     byte questionMark = 63; 
     agentSerial.BaseStream.WriteByte(questionMark); 

     System.Threading.Thread.Sleep(100); 
     agentSerial.BaseStream.Read(result, 0, 7); 

     string system = ""; 
     foreach (byte letter in result) 
     { 
      system += Convert.ToChar(letter).ToString(); 
     } 

     bool read = readPort(); 

     if (read) 
     { 
      int i = 1; 
      foreach (byte value in data) 
      { 
       storeData(i, value); 
       i++; 
      } 
     } 

     MessageBox.Show(resultMessage, "Status Message", MessageBoxButton.OK, MessageBoxImage.Information); 
    } 

    bool readPort() 
    { 
     bool succesfulRead = false; 

     agentSerial.BaseStream.Flush(); 
     agentSerial.BaseStream.Write(Global.READ_VALUES, 0, Global.READ_VALUES.Length); //begin read values 

     byte key = (byte)agentSerial.BaseStream.ReadByte(); 

     if (Global.START_COMMAND == key) //Verify continue key 
     { 
      for (int i = 0; i < 2246; i++) //Loop through values 
      { 
       try 
       { 
        data[i] = (byte)agentSerial.BaseStream.ReadByte(); 
        agentSerial.BaseStream.Write(Global.GO_AHEAD, 0, Global.GO_AHEAD.Length); 
        agentSerial.BaseStream.Flush(); 
       } 
       catch 
       { 
        resultMessage = "An error occured, while reading the device settings." + i; 
        break; 
       } 
      } 
      if (data[2245] != Global.FINISH_COMMAND) 
      { 
       resultMessage = "An error occured, while reading the device settings." + 2245; 
      } 
      else 
      { 
       succesfulRead = true; 
      } 
     } 
     else //Key failed and displays error 
     { 
      resultMessage = "An error occured, are you sure you are trying to connect to an Agent (Radio Version)? If so make sure you have followed the steps listed above."; 
     } 

     return succesfulRead; 
    } 

    void storeData(int iteration, byte value) 
    { 
     if (iteration > 0 && iteration < 385) //read schedule 
     { 
      double pos = (iteration - 1)/48; 

      int i = (int)Math.Floor(pos); 
      int j = (iteration - 1) - (i * 48); 

      Create.schedule[i, j] = value; 
     } 

     if (iteration > 384 && iteration < 1285) //read alarm history 
     { 
      double pos = (iteration - 385)/9; 

      int i = (int)Math.Floor(pos); 
      int j = (iteration - 385) - (i * 9); 

      Create.alarms[i, j] = value; 
     } 

     if (iteration > 1284 && iteration < 1345) //read error log 
     { 
      double pos = (iteration - 1285)/6; 

      int i = (int)Math.Floor(pos); 
      int j = (iteration - 1285) - (i * 6); 

      Create.errors[i, j] = value; 
     } 

     if (iteration > 1344 && iteration < 1945) //read voltage history 
     { 
      double pos = (iteration - 1345)/6; 

      int i = (int)Math.Floor(pos); 
      int j = (iteration - 1345) - (i * 6); 

      Create.voltage[i, j] = value; 
     } 

     if (iteration > 1944 && iteration < 1973) //read holidays 
     { 
      Create.holidays[iteration - 1945] = value; 
     } 

     if (iteration > 1972 && iteration < 2168) //read message sequences 
     { 
      double pos = (iteration - 1973)/15; 

      int i = (int)Math.Floor(pos); 
      int j = (iteration - 1973) - (i * 15); 

      Create.messages[i, j] = value; 
     } 

     if (iteration > 2167 && iteration < 2196) //read message info 
     { 
      Create.recordings[iteration - 2168] = value; 
     } 

     if (iteration > 2195 && iteration < 2246) //read sysval 
     { 
      Create.sysval[iteration - 2196] = value; 
     } 

     if (iteration == 2246 && value == Global.FINISH_COMMAND) 
     { 
      if (Global.restoring) 
      { 
       resultMessage = "Your device has been succesfully restored."; 
      } 
      else 
      { 
       resultMessage = "Your device has been succesfully connected, and all settings have been loaded from the device."; 
      } 
     } 
     else 
     { 
      resultMessage = "An error occured, while reading the device settings." + "Storing"; 
     } 
    } 

    void reportProgress(object sender, ProgressChangedEventArgs e) 
    { 

    } 
} 

回答

0

我會做全面的其他方式。 從agentRadio窗口內初始化工作線程。
具有與agentRadioRead類中的設備互動有關的所有功能。
取出引用表單窗口的所有代碼。 創建此agentRadioRead類的對象並將其傳遞給工作線程。 使用workerthread的reportprogress事件將狀態更改報告給agentRadio窗口。 讓agentRadio中的事件處理程序隨後執行任何UI更新。