nattsurfaren

Just doing a post on this so I can go back for update and to get comments from you.

So I have been having problems with static functions when they where forced upon you or required.
And especially when supporting threading this can be a headache.

Lets say you just want to update a textbox that maintain a log. So how do you that from a different thread in a static function? (pain in the ass)

Here is how you can do this:

static int Add(int x, int y) //Note this is static
        {

//d can update textbox
            Add2TextBoxDelegate d = new Add2TextBoxDelegate(Add2TextBox);
            d.BeginInvoke(" Add() invoked on thread " + Thread.CurrentThread.GetHashCode(), null, null);
         
            Thread.Sleep(5000);
            d.BeginInvoke(" done", null, null);
            return x + y;
        }
        public delegate void Add2TextBoxDelegate(String str);
        public static Add2TextBoxDelegate Add2TextBox; //Static important

        private void Add2TextBoxMethod(String str)
        {
            if (textBox1.InvokeRequired)
            {
                Add2TextBox = new Add2TextBoxDelegate(Add2TextBoxMethod);
                this.Invoke(Add2TextBox, str);
            }
            else
            {
                textBox1.Text += str;
            }
        }

        public Form1()
        {
            InitializeComponent();

        }

        private void button1_Click(object sender, EventArgs e)
        {
            Main();
        }
        public delegate int BinaryOp(int x, int y);

        public void Main()
        {
            textBox1.Text+=" ***** Synch Delegate Review *****";
            textBox1.Text += " Main() invoked on thread " + Thread.CurrentThread.GetHashCode();

            BinaryOp b = new BinaryOp(Add);
            IAsyncResult iftAR = b.BeginInvoke(10, 10, null, null);

            textBox1.Text += " Doing more work in Main()!";
        }
       
     

        private void Form1_Load(object sender, EventArgs e)
        {
            //Very important for this to work.
            Add2TextBox = new Add2TextBoxDelegate(Add2TextBoxMethod);
        }
    }

nop_90

Do not feel like looking at ur code.
But i can see u are attempting to use GUI with threads.

<>ALL GUI's are not thread safe
even if they tell you they are, they are not. (this hold true for all languages)
objects in the gui can only be called with the thread that created them.
if you attempt to call from another threads 99% of the time it works, and then u get wierd mysterious crashes.

So how do u update the gui.
2 methods.

1 use the gui's event system (assuming that the gui has one wxwindows has, windows has)
each worker thread passes info using events to main thread which updates gui.

2 use queues (thread safe ones), if language does not have make ur own.
each worker thread add events to the queue, main thread polls the queue, and then updates gui.
(this is actually a variant of number 1 method)
i use this method since you have more control. also event system should be thread safe, but you could have some shared variables which can cause nasty, this eliminates it.
also as rule of thumb, share as few variables as possible.
with the queue system you should only have to share 1 queue.
also even if they tell u library is thread safe, assume they are lying Applause




Perkiset's Place Home   Politics @ Perkiset's