Using async/await function

ChanChan 1 Reputation point
2022-06-22T08:54:59.803+00:00

Having trouble using async/await for my Application causing high cpu usage

private void loadit()  
    {  
        server = new SimpleTcpServer();  
        server.Delimiter = 0x13;  
        server.StringEncoder = Encoding.UTF8;  
        server.DataReceived += server_datarx;  
        backgroundworker1 = new BackgroundWorker();  
        backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);  
        INCOMING = new Thread(INCOMINGTHREAD);  
  
    }  
  
    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)  
    {  
        while (true)  
        {  
            if(rxv)  
            {  
                rxv = false;  
                INCOMING = new Thread(INCOMINGTHREAD);  
                INCOMING.Start();  
            }  
            if (backgroundWorker1.CancellationPending)  
            {  
                e.Cancel = true;  
                return;  
            }  
        }  
    }  
  
    private void server_datarx(object sender, SimpleTCP.Message e)  
    {  
        rxvmsg = e.MessageString;  
        rxv = true;  
        SetText(rxvmsg);  
        e.ReplyLine("\u0006");  
    }  
  
    delegate void SetTextCallback(string text);  
    private void SetText(string text)  
    {  
        if (this.textBox2.InvokeRequired)  
        {  
            SetTextCallback d = new SetTextCallback(SetText);  
            this.Invoke(d, new object[] { text });  
        }  
        else  
        {  
            if (text != null)  
            {  
                this.textBox2.AppendText(Environment.NewLine);  
                this.textBox2.AppendText(text);  
            }  
        }  
    }  
    private void button1_Click(object sender, EventArgs e)  
    {  
        if((textBox1.Text == "") || (textBox3.Text == ""))  
        {  
            MessageBox.Show("All fields are required","System Notification",MessageBoxButtons.OK,MessageBoxIcon.Information);  
        }  
        else  
        {  
            Cursor.Current = Cursors.WaitCursor;  
            startit();  
            Cursor.Current = Cursors.Default;  
  
        }  
    }  
  
    private void startit()  
    {  
        if (button1.Text == "Start")  
        {  
            con = new SqlConnection(textBox3.Text);  
            if(connectionchecker())  
            {  
                backgroundWorker1.RunWorkerAsync();  
                server.Start(Convert.ToInt32(textBox1.Text));  
                button1.Text = "Stop";  
                SetText("Established Database Connection");  
                settingslocker(true);  
                  
  
            }  
            else  
            {  
                MessageBox.Show("Please check database connection", "Database connection error", MessageBoxButtons.OK, MessageBoxIcon.Information);  
            }  
        }  
        else if (button1.Text == "Stop")  
        {  
            server.Stop();  
            button1.Text = "Start";  
            settingslocker(false);  
            if (backgroundWorker1.IsBusy)  
            {  
                backgroundWorker1.CancelAsync();  
            }  
        }  
    }  
  
    private void settingslocker(bool indi)  
    {  
        if(indi)  
        {  
            textBox1.Enabled = false;  
            textBox3.Enabled = false;  
        }  
        else  
        {  
            textBox1.Enabled = true;  
            textBox3.Enabled = true;  
        }  
    }  
  
    private bool connectionchecker()  
    {  
        try  
        {  
            con.Open();  
            con.Close();  
            return true;  
        } catch  
        {  
            return false;  
        }  
    }  
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
7,022 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 28,281 Reputation points
    2022-06-22T15:57:36.103+00:00

    this code is poorly written:

    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)  
         {  
             while (true)  
             {  
                 if(rxv)  
                 {  
                     rxv = false;  
                     INCOMING = new Thread(INCOMINGTHREAD);  
                     INCOMING.Start();  
                 }  
                 if (backgroundWorker1.CancellationPending)  
                 {  
                     e.Cancel = true;  
                     return;  
                 }  
             }  
         }  
    

    if rev is false, it just a compute loop (100% thread cpu). also if canceled, it does not stop the thread it starts.

    No comments