The Cache: Technology Expert's Forum
 
*
Welcome, Guest. Please login or register. September 22, 2019, 05:25:27 PM

Login with username, password and session length


Pages: [1]
  Print  
Author Topic: Static functions in threading enviroment (for newbies)  (Read 10328 times)
nattsurfaren
Journeyman
***
Offline Offline

Posts: 64


View Profile
« on: April 20, 2008, 02:58:05 PM »

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:

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

//d can update textbox
            Add2TextBoxDelegate d = new Add2TextBoxDelegate(Add2TextBox);
            d.BeginInvoke("\r\nAdd() invoked on thread " + Thread.CurrentThread.GetHashCode(), null, null);
           
            Thread.Sleep(5000);
            d.BeginInvoke("\r\ndone", 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+="\r\n***** Synch Delegate Review *****";
            textBox1.Text += "\r\nMain() invoked on thread " + Thread.CurrentThread.GetHashCode();

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

            textBox1.Text += "\r\nDoing more work in Main()!";
        }
       
       

        private void Form1_Load(object sender, EventArgs e)
        {
             //Very important for this to work.
            Add2TextBox = new Add2TextBoxDelegate(Add2TextBoxMethod);
        }
    }
« Last Edit: April 20, 2008, 03:01:11 PM by nattsurfaren » Logged
nop_90
Global Moderator
Lifer
*****
Offline Offline

Posts: 2203


View Profile
« Reply #1 on: April 20, 2008, 05:01:15 PM »

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 Smiley



Logged
nattsurfaren
Journeyman
***
Offline Offline

Posts: 64


View Profile
« Reply #2 on: April 21, 2008, 01:42:46 PM »

Thanks for your response nop_90.
Based on your information and the information on threads I'm currently reading I did a test how the delegate function worked.
So an access to a windows gui control should only be made by the thread creating it.
Look at the nice thing with this code as seen above:
Code:
if (textBox1.InvokeRequired)
            {....}
This is called if any thread that is not the original thread owning the control is currently running.
So the goal here is to rerun the function but with the original thread.
So when the function is called again with the original thread then
the textbox is updated.
I don't know how Atomic these operations is but it is a step in the right direction i suppose, and convenient.
Using thread-volatile operations is hopefully eliminated in this way.
Well I'll bet I will eat my optimistic words in a later stage but that will be then.
 
Logged
Pages: [1]
  Print  
 
Jump to:  

Perkiset's Place Home   Best of The Cache   phpMyIDE: MySQL Stored Procedures, Functions & Triggers
Politics @ Perkiset's   Pinkhat's Perspective   
cache
mart
coder
programmers
ajax
php
javascript
Powered by MySQL Powered by PHP Powered by SMF 1.1.2 | SMF © 2006-2007, Simple Machines LLC
Seo4Smf v0.2 © Webmaster's Talks


Valid XHTML 1.0! Valid CSS!