Background worker freezes / dies while running

BrianH

This may be a hardware issue, as I'm not exactly sure how background workers are implemented, but here is the issue: I have 3 background workers running different threads. I've coded those threads to display when they start and end, and I am getting some very strange results.

On some executions, all 3 threads will start, but only 1 will end (and I've waited many minutes to see if the program was just executing very slow, and put breaks in the code - it's not running slow, the background worker just "dies"). Other executions I get 2 threads to finish. Other times I get only 2 threads starting, and both end, etc.

I am not changing the code (entered below) at all between runs, I am simply stopping the program and restarting it. As such, I suspect it may be a hardware issue (CPU maybe?). Unfortunately, I don't have another PC to test the program on right now, and the PC that I am using is ... "far from the best".

So, my question: Could there be hardware issues that cause a background worker (and the thread that it is working on) to just die off / not start at all? Or does it absolutely have to be something in the code?

(The following is obviously not really my code. It is a much simpler version that illustrates the problem) Code:

public partial class Form1 : Form
{
    BackgroundWorker[] workers = new BackgroundWorker[3];
    Stopwatch timeKeeper = new Stopwatch();
    string text = "";

    public Form1()
    {
        timeKeeper.Start();

        InitializeComponent();

        for (int i = 0; i < 3; i++)
        {
            workers[i] = new BackgroundWorker();
            workers[i].DoWork += new System.ComponentModel.DoWorkEventHandler(this.BWWorker_DoWork);
            workers[i].RunWorkerCompleted += new System.ComponentModel.RunWorkerCompletedEventHandler(this.BWWorker_WorkDone);
            workers[i].RunWorkerAsync(i);
        }
    }

    private void BWWorker_WorkDone(object sender, RunWorkerCompletedEventArgs e)
    {
        this.textBox1.Text = text;
    }

    private void BWWorker_DoWork(object sender, DoWorkEventArgs e)
    {
        switch (e.Argument.ToString())
        {
            case "0":
                startThread1();
                break;

            case "1":
                startThread2();
                break;

            case "2":
                startThread3();
                break;
        }
    }

    void startThread1()
    {
        text += Environment.NewLine + "Starting thread 1" + Environment.NewLine + "at " + timeKeeper.Elapsed.ToString();

        this.textBox1.Text = text;

        for (int i = 0; i < 25000; i++)
        {

        }

        text += Environment.NewLine + "Ending thread 1" + Environment.NewLine + "at " + timeKeeper.Elapsed.ToString();
    }

    void startThread2()
    {
        text += Environment.NewLine + "Starting thread 2" + Environment.NewLine + "at " + timeKeeper.Elapsed.ToString();

        this.textBox1.Text = text;

        for (int i = 0; i < 10000; i++)
        {

        }

        text += Environment.NewLine + "Ending thread 2" + Environment.NewLine + "at " + timeKeeper.Elapsed.ToString();
    }

    void startThread3()
    {
        text += Environment.NewLine + "Starting thread 3" + Environment.NewLine + "at " + timeKeeper.Elapsed.ToString();

        this.textBox1.Text = text;

        for (int i = 0; i < 75000; i++)
        {

        }

        text += Environment.NewLine + "Ending thread 3" + Environment.NewLine + "at " + timeKeeper.Elapsed.ToString();
    }
}

The result on 3 back-to-back runs with no code changes is as follows : (The 3rd pic runs all 3, as desired. The first 2 run/finish some of the threads, but not all (again, I've let the program run for minutes, so I know it's not just running slow) ... ?)

enter image description here

Eric J.

So, my question: Could there be hardware issues that cause a background worker (and the thread that it is working on) to just die off / not start at all? Or does it absolutely have to be something in the code?

That is very, very unlikely.

I suspect the real issue is that you are updating text from multiple theads. Concatenating strings like this is not a thread-safe operation, and the threads can be expected to complete at about the same time. Even though the loops have a different number of iterations, minor fluctuations in thread scheduling can cause them to step on each other. In particular, the line

text += Environment.NewLine + "Ending thread 1" + Environment.NewLine + "at " + timeKeeper.Elapsed.ToString();

could run such that

  • Thread 1 reads the current value of text.
  • Thread 1 is interrupted
  • Thread 2 is scheduled and also reads the current value of text
  • Thread 2 updates the value in text
  • Thread 2 is interrupted
  • Thread 1 is scheduled. It overwrites text, without re-reading the change just made by Thread 2.

Try using lock statements around changes to resources shared by the threads.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Spark worker dies after running for some duration

From Dev

Spark worker dies after running for some duration

From Dev

While loop background worker broken

From Dev

Macbook Air freezes while running Ubuntu 13.10

From Dev

Macbook Air freezes while running Ubuntu 13.10

From Dev

Background Worker is Running but Doesn't Print anything

From Dev

Reporting progress on a WPF background worker running a WCF service

From Dev

JavaScript Worker : how to check if a message was received while running expensive task

From Dev

Got an error while running web api project on azure worker role

From Dev

Running commands in the background while another is processing?

From Dev

Repeated Background Query while app is running

From Dev

How to stop a infinite while loop running in background?

From Dev

How to stop a bash while loop running in the background?

From Dev

react native crash in ios while running in background

From Dev

Running code in main thread in background thread while blocking background thread

From Dev

Running code in main thread in background thread while blocking background thread

From Dev

Background Worker Running Twice on bw_DoWork() and bw_ProgressChanged() when using textBox1.AppendText()

From Dev

Field grouping in case worker dies in Storm

From Dev

Send Local Notifications while App is running in the background Swift 2.0

From Dev

Error when running a script (with a while Loop) in a script created background process

From Dev

C# Threading/Async: Running a task in the background while UI is interactable

From Dev

Can you check trial while app is running in background

From Dev

Sending Data To Server While App is Running in Background in iOS 7

From Dev

C# Threading/Async: Running a task in the background while UI is interactable

From Dev

python running task in the background while allowing tkinter to be active

From Dev

how to spawn a separate process in the background while running a foreground process

From Dev

Running a while loop in a separate thread to check for background tasks

From Dev

Running a bash script with an infinite while loop at startup and in the background on a Raspberry Pi

From Dev

Repeating a task every x seconds while running in background

Related Related

  1. 1

    Spark worker dies after running for some duration

  2. 2

    Spark worker dies after running for some duration

  3. 3

    While loop background worker broken

  4. 4

    Macbook Air freezes while running Ubuntu 13.10

  5. 5

    Macbook Air freezes while running Ubuntu 13.10

  6. 6

    Background Worker is Running but Doesn't Print anything

  7. 7

    Reporting progress on a WPF background worker running a WCF service

  8. 8

    JavaScript Worker : how to check if a message was received while running expensive task

  9. 9

    Got an error while running web api project on azure worker role

  10. 10

    Running commands in the background while another is processing?

  11. 11

    Repeated Background Query while app is running

  12. 12

    How to stop a infinite while loop running in background?

  13. 13

    How to stop a bash while loop running in the background?

  14. 14

    react native crash in ios while running in background

  15. 15

    Running code in main thread in background thread while blocking background thread

  16. 16

    Running code in main thread in background thread while blocking background thread

  17. 17

    Background Worker Running Twice on bw_DoWork() and bw_ProgressChanged() when using textBox1.AppendText()

  18. 18

    Field grouping in case worker dies in Storm

  19. 19

    Send Local Notifications while App is running in the background Swift 2.0

  20. 20

    Error when running a script (with a while Loop) in a script created background process

  21. 21

    C# Threading/Async: Running a task in the background while UI is interactable

  22. 22

    Can you check trial while app is running in background

  23. 23

    Sending Data To Server While App is Running in Background in iOS 7

  24. 24

    C# Threading/Async: Running a task in the background while UI is interactable

  25. 25

    python running task in the background while allowing tkinter to be active

  26. 26

    how to spawn a separate process in the background while running a foreground process

  27. 27

    Running a while loop in a separate thread to check for background tasks

  28. 28

    Running a bash script with an infinite while loop at startup and in the background on a Raspberry Pi

  29. 29

    Repeating a task every x seconds while running in background

HotTag

Archive