Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8628655
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T08:36:09+00:00 2026-06-12T08:36:09+00:00

I am calling my background worker by the following code: private void UpdateDataTimer_Tick(object sender,

  • 0

I am calling my background worker by the following code:

private void UpdateDataTimer_Tick(object sender, EventArgs e)
{
    if (!serialPortWorker.IsBusy)
    {
        serialPortWorker.RunWorkerAsync();
    }
}

My DoWork event is as follows:

private void serialPortWorker_DoWork(object sender, DoWorkEventArgs e)
{
    //Configures serial port
    connection.BaudRate = 19200;
    connection.DataReceived += new SerialDataReceivedEventHandler(DataReceivedEvent);

    //Sends the commands for opening diagnostics
    string[] init_commands = { "STRING", "STRING", "STRING", "STRING", "STRING" };
    foreach (string command in init_commands)
    {
        connection.WriteLine(command + connection.NewLine);
        Thread.Sleep(1000);
    }

    const string constant_message_section = "G03";
    string[] command_list = { "62", "64", "5C" };

    //Writes all commands to all radio addresses
    foreach (int address in radioAddresses)
    {
        foreach (string command in command_list)
        {
            for (int i = 0; i < MAX_ATTEMPTS; i++)
            {
                connection.WriteLine(constant_message_section + address.ToString("X4") + command);
                Thread.Sleep(500);
            }
        }
    }

    Thread.Sleep(1000); //Give a little time for all responses to come in
}

For some reason, after a few hundred calls of the UpdateDataTimer_Tick event, it will not run the serialPortWorker any more. I put a debugger at if (!serialPortWorker.IsBusy), and it indicated that the serialPortWorker is still busy. It must be hanging up somewhere in the DoWork event, right? Any ideas why?

For those that are interested, the data received event is as follows:

public void DataReceivedEvent(object sender, SerialDataReceivedEventArgs e)
{
    SerialPort sp = (SerialPort)sender;
    string receive = sp.ReadLine();

    try
    {
        Debug.Logger.WriteToDebug("Data Received Serial Port: " + receive);
    }
    catch { }

    try
    {
        int unit_address = Int32.Parse(receive.Substring(1, 4), System.Globalization.NumberStyles.HexNumber);

        if (radioAddresses.Contains(unit_address))
        {
            int radio_index = radioAddresses.IndexOf(unit_address) + 1;
            int max_index = radio_index * 3;

            integrityMonitor[radio_index] = DateTime.Now; //Last updated time

            int message_data = 0;

            if (receive.Contains("66"))
            {
                //Stuff
            }
            else if (receive.Contains("61"))
            {
                //Stuff
            }
            else if (receive.Contains("55"))
            {
                //Stuff
            }
        }
    }
    catch { }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T08:36:10+00:00Added an answer on June 12, 2026 at 8:36 am

    Ok, since nobody has left an answer, I will. The issue was the line

    connection.DataReceived += new SerialDataReceivedEventHandler(DataReceivedEvent);
    

    being called during every tick of the timer in background worker. This would cause numerous instances of the event handler, which would eventually cause the background worker to lock up and report busy all the time. To correct this problem, I needed to put in

    connection.DataReceived -= new SerialDataReceivedEventHandler(DataReceivedEvent);
    

    in order to avoid having numerous event handlers processing the data received event. This has resolved my problem.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got the following code: void ReferenceManager_DoWork(object sender, DoWorkEventArgs e) { try { //
Is there a way to directly restart a background worker? Calling CancelAsync() followed by
Am calling the following code from backgroundworker, but instead of setting the desired text,
Calling the WriteObject Method from a background thread is not possible! Is there a
//calling class import javax.swing.JFrame; class jcheckkbox { public static void main(String args[]) { jRadio
I have the following set up: BackgroundWorker backgroundInstancesWorker = new BackgroundWorker(); backgroundInstancesWorker.DoWork += new
I want to put this method into background worker class, i am trying but
Is doing this less efficient than setting up a background worker thread? Because it
I have got a C# user control, which has got it's own background worker
I'm using a background-worker to do all of my processing. Inside there, I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.