2013-02-23 43 views
0

我有一個微芯片mcp2200設備。設備連接樹形按鈕(作爲數字輸入)。 我用它作爲反饋機器。 machine schemantics
我將它附加到Windows Server 2003 x64。然後,我在我的C#應用​​程序中使用了微芯片mcp2200驅動程序和Managed SimpleIO DLL。
在C#中,我使用頻率爲500毫秒的時間來檢查按鈕是否被按下。在一兩個小時之後,服務器的CPU使用率一直很高。如果我殺了C#程序,一切都好。我如何愛cpu使用?或者我的代碼有錯誤?
一些代碼:c#microchip mcp2200高CPU使用率

void timer1_Tick(object sender, EventArgs e) 
{ 
    if (DateTime.Now.ToString("HH") == _turnOffApp) Environment.Exit(0); //after working hours, turn off app. Task scheduler will start it at 8 AM 
    if (btn_down == 99) 
    { //if button is UP 
     bool connStatus = SimpleIOClass.IsConnected(); 
     if (connStatus) 
     { 
      lblConnStatus.Text = "Connected"; 
      unsafe 
      { 
       uint rez1 = 2; 
       uint* rez = &rez1; 

       for (uint i = 0; i < 8; i++) 
       { 
        if (i == 5 || i == 7) continue; //unused pins 
        rez1 = 2; 
        if (SimpleIOClass.ReadPin(i, rez)) 
        { 
         string rez11 = rez1.ToString(); 
         this.Controls["label" + i.ToString()].Text = rez1.ToString(); 
         if (rez1.ToString() == "0") // 0 = button down, 1 = button up 
         { 
          RegisterButton(i); 
          i = 8; 
          continue; 
         } 
        } 
        else 
        { 
         try { this.Controls["label" + i.ToString()].Text = "ReadPinErr"; } 
         catch { } 
        } 
       } 
      } 
     } 
     else 
     { 
      lblConnStatus.Text = "NOT Connected"; 
     } 
    }//end btn_down == 99 
} 


void RegisterButton(uint poga) 
{ 
    btn_down = poga; 
    device = Device(poga); 
    CheckUserInput(); 
} 


void SendBtnToServer(string btn) 
{ 
    try 
    { 
     string uri = @"http://web-server.lan/pogas.php?device="+ device+ "&p=" + btn; 
     WebClient clietn = new WebClient(); 
     clietn.Headers.Add("Cache-Control", "no-cache"); 
     clietn.CachePolicy = new System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.NoCacheNoStore); 
     clietn.DownloadString(uri); 
     clietn.Dispose(); 
    } 
    catch (Exception ex) { try { File.AppendAllText(logFails, DateTime.Now.ToString() + " " + ex.ToString() + "\n\r"); } catch { } } 
} 


void CheckUserInput() 
{ 
    Thread.Sleep(2000); //wait 2 sec until user releases button 
    bool connStatus = SimpleIOClass.IsConnected(); 

    if (connStatus) 
    { 
     lblConnStatus.Text = "Connected"; 
     unsafe 
     { 
      uint rez1 = 2; 
      uint* rez = &rez1; 
      string ctrName = "label" + btn_down.ToString(); 
      if (SimpleIOClass.ReadPin(btn_down, rez)) 
      { 
       if (rez1.ToString() == "1") // poga atlaista 
       { 
        // register button press 
        if (btn_down == Pogas["smaids"]) { OSD(":)"); new Thread(() => SendBtnToServer("1")).Start(); } 
        if (btn_down == Pogas["neitrals"]) { OSD(":|"); new Thread(() => SendBtnToServer("2")).Start(); } 
        if (btn_down == Pogas["bedigs"]) { OSD(":("); new Thread(() => SendBtnToServer("3")).Start(); } 

        if (btn_down == Pogas["smaids2"]) { OSD(":)"); new Thread(() => SendBtnToServer("1")).Start(); } 
        if (btn_down == Pogas["neitrals2"]) { OSD(":|"); new Thread(() => SendBtnToServer("2")).Start(); } 
        if (btn_down == Pogas["bedigs2"]) { OSD(":("); new Thread(() => SendBtnToServer("3")).Start(); } 
       } 
      } 
      else this.Controls[ctrName].Invoke(new Action(() => this.Controls[ctrName].Text = "Read pin ERROR (Release)")); 
     } 
    } 
    else 
    { 
     lblConnStatus.Text = "NOT Connected"; 
    } 
    btn_down = 99; 
}// CheckUserInput 
+0

從一開始或一段時間以後,它是否具有較高的CPU權利? – 2013-02-23 14:37:27

+0

過了一段時間。 – Guntis 2013-02-23 16:36:29

+0

然後附加一個調試器,它具有較高的CPU並將其分解,以查看引起問題的調用堆棧。如果高CPU進程也是系統,那麼它可能是芯片的設備驅動程序問題。那麼你應該看看更新的驅動程序。 – 2013-02-23 20:31:06

回答

0

答案是:C#應用程序重啓通過計劃作業每隔一小時。沒有更高的CPU使用率。