0

Context: I have a form with multiple checkbox "buttons".
In mainform I have a do-while cycle that needs to check form.checkBox.Checked in real time to update some variables.
I tried running the cycle inside an async worker, threading the form2, running the cycle inside the form2, all unsuccessfully.

Faulty Code:

namespace CSharp
{
    public partial class MainForm : Form
    {
        bool exit_state = true;
        private BackgroundWorker worker = null;
        public MainForm()
        {
            InitializeComponent();
        }
        private void bRecord_Click(object sender, EventArgs e)
        {
            System.IO.StreamWriter file = new System.IO.StreamWriter(AppDomain.CurrentDomain.BaseDirectory + DateTime.Now.ToString("yyyy-dd-M--HH-mm-ss") + ".xml", true);
            string behavior = null;
            exit_state = false;
            form2 form = new form2();
            form2.ShowDialog();
            worker = new BackgroundWorker();
            worker.WorkerSupportsCancellation = true;
            worker.DoWork += new DoWorkEventHandler((state, args) =>
            {
                do
                {
                    if (form2.checkBox1.Checked) behavior = form2.checkBox1.Text;
                    if (form2.checkBox2.Checked) behavior = form2.checkBox2.Text;
                    file.WriteLine(behavior);
                }
                while(exit_state==false);
            });
        }
        private void bStop_Click(object sender, EventArgs e)
        {
            exit_state = true;
            _worker.CancelAsync();
        }
    }
}

I believe this is solved using threads for each form, but I lack the knowledge about threads to know how.

EDIT: I realize now that I was not explicit enough and was asking the wrong question so I started a new one.

Community
  • 1
  • 1
wmw301
  • 49
  • 1
  • 8
  • Your worker thread is accessing UI elements which is dangerous and harmful to kittens –  Nov 06 '16 at 15:23
  • 2
    Why aren't you using `CheckedChanged` event to do this? – InBetween Nov 06 '16 at 15:41
  • Because when form2.ShowDialog(); is launched the code that follows is not run simultaneously to the form, which is what I was looking for. Also I need to differentiate if the checkbox is "pressed" or not. – wmw301 Nov 06 '16 at 15:44
  • 2
    Also, it's never a good idea to try and solve a problem by _throwing threads at it_ to make it more complex particularly when new to threading concepts –  Nov 06 '16 at 15:51
  • The worker is there to solve a different problem, not related to my question. It's there to allow to break the do-while if a button in MainForm is pressed. – wmw301 Nov 06 '16 at 15:58
  • _"worker is there to solve a different problem, not related to my question"_ -- then, what is your question? The entire description in your post focuses on the loop in your worker. So if not that, then what? It is not clear from your post what it is you actually need help with. The code you posted is wrong in several ways: using `BackgroundWorker` for a long- or indefinitely-running operation is wrong; calling `BackgroundWorker.CancelAsync()` when your `DoWork` handler doesn't use `CancellationPending` is wrong; ... – Peter Duniho Nov 06 '16 at 19:13
  • ... exposing actual `Control` members (e.g. `checkBox1` and `checkBox2`) instead of wrapping their relevant properties is wrong; expecting the user to be able to click the `bStop` button when you are using `ShowDialog()` (which prevents input to any other form) is wrong. So there's lots to critique and provide alternatives to in your question. But is any of that what you actually want help with? What _do_ you want help with? – Peter Duniho Nov 06 '16 at 19:13
  • As I see it, what you really need help with here is handling the interaction between `MainForm` and `form2`. See the marked duplicate for answers on how to do that. Even [the accepted answer](http://stackoverflow.com/a/1665688) correctly shows wrapping control properties to encapsulate the control object itself, and I've provided [an answer](http://stackoverflow.com/a/29872737) to that question that shows how to further decouple the classes by using events to allow one form to respond to e.g. changes in property values of the other form. – Peter Duniho Nov 06 '16 at 19:18
  • I see no need to use any sort of threading here, not `BackgroundWorker` nor any other concurrency technique. By following the advice in the marked duplicate, you can avoid all of the wrong things with your current code which I described above. – Peter Duniho Nov 06 '16 at 19:18
  • @PeterDuniho To clarify the hole worker thing was a way to stop the loop via the press of a button and it was taken from this [answer](http://stackoverflow.com/a/27580333). I'm still reviewing the info in the marked duplicate, will report back after I learn more. – wmw301 Nov 07 '16 at 18:53
  • 1
    Your loop appears to be doing nothing more than polling the `Checked` property values. Polling is bad. Use events. Then you don't need the loop, and then you don't need a way to _stop_ the loop. – Peter Duniho Nov 07 '16 at 19:12

0 Answers0