0

I a have microchip mcp2200 device. To device is attached tree buttons (as digital inputs). I use it as feedback machine. machine schemantics
I attached it to windows server 2003 x64. Then i used microchips mcp2200 driver and Managed SimpleIO DLL in my C# app.
In C# i use time with frequency 500 ms to check if button was pressed. After hour or two server has constant high cpu usage. If i kill C# program, all is ok. How can i lover cpu usage? Or my code has wrong?
Some code:

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
Guntis
  • 496
  • 1
  • 5
  • 19
  • Is it having high CPU right from the beginning on or after some time? – Alois Kraus Feb 23 '13 at 14:37
  • Then attach a a debugger while it has high CPU and break it to see the call stack where it was causing issues. If the high CPU process was also System then it is likely a device driver issue with your chip. Then you shuould have a look at updated drivers. – Alois Kraus Feb 23 '13 at 20:31
  • cpu usage is not with my app, but with system process. Looks like it is driver related. – Guntis Feb 24 '13 at 11:08

1 Answers1

0

Answer is: C# app is restarted every hour via Scheduled job. No more high cpu usage.

Guntis
  • 496
  • 1
  • 5
  • 19