Thread Safe Invoke Functions


Download Project
Every developer who using threads  encounter with Illegal Cross Thread operations. And they solve this probles with creating delegate functions and invoking by other control. And this action cause writting more code. In this post i introduce you  an InvokeService classes that using methods with threadsafe operations.

Before this we make a method thread safe like this and think about all methods you do it like this way it causes lots of codes:

       //This code is not nessasary now
        public delegate void AppendTextDelegate(string text);
        public void GetSomeOutsiteData(string data)
        {
            this.Invoke(new AppendTextDelegate(AppendText), data);
        }
        //The method that use for thread safe
        public void AppendText(string text)
        {
            textBox1.Text += text + Environment.NewLine;
        }

Now just one line:

Instance.RegisterThreadSafeWatcher("AppendText", this,(Delegatenew Server.DataRecievedDelegate(AppendText));

Usage :

“Test” Class
First you create a business class that  inherits  form “InvokableBase”.And two sample delegate method that used by this class.Also this class contains a different thread for doing its jobs will cause “Cross Thread operation”.For doing this we use Invoke() function of  base class of InvokableBase

    class Test:InvokableBase,IDisposable
    {
        public delegate void TextDelegate(string text);
        public delegate void IntDelegate(int text);
        Thread a = null;
        public void DoSomething()
        {
           a = new Thread(new ThreadStart(worker));
            a.Start();
        }
        public void worker()
        {
            for (int i = 0; i < 10; i++)
            {
                string text = "Hello World";
                base.Invoke(typeof(TextDelegate), text);
                Thread.Sleep(1000);
            }
            for (int i = 0; i < 10; i++)
            {
                base.Invoke(typeof(IntDelegate), i);
                Thread.Sleep(1000);
            }
        }

A Windows Form Class This class can used by main thread for complete its GUI operations. Test class sends text and integer data to this form class and this forms safely prints it into a multiline textbox and using InvokeService prevents errors of cross thread operations.

        Test cls =null;
        private void button1_Click(object sender, EventArgs e)
        {
            cls = new Test();
            cls.RegisterThreadSafeWatcher("AppendText", this,(Delegate) new Test.TextDelegate(AppendText));
            cls.RegisterThreadSafeWatcher("AppendInt", this, (Delegate) new Test.IntDelegate(AppendInt));
            cls.DoSomething();
        }
        public void AppendText(string value)
        {
            textBox1.Text += value + Environment.NewLine;
        }
        public void AppendInt(int value)
        {
            textBox1.Text += value.ToString() + Environment.NewLine;
        }
        private void TestForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            cls.Dispose();
        }
You can download full project from this link:
http://rapidshare.com/files/308290393/InvokeService.rar
Have a nice coding !
Advertisements

About bpostaci
Sr. Support Escalation Engineer in Microsoft.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: