Tuesday 14 June 2016

c# delegate

A delegate is a reference to a method. Whereas objects can easily be sent as parameters into methods, constructor or whatever, methods are a bit more tricky. But every once in a while you might feel the need to send a method as a parameter to another method, and that's when you'll need delegates.

https://msdn.microsoft.com/en-CA/library/aa288459(v=vs.71).aspx
http://csharpindepth.com/Articles/Chapter2/Events.aspx


-----------------------------------------------------------------------------------
synchronous simple example

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;

namespace delegate1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        delegate string my_delegate(ref int x);

        private static int num = 0;

private void button1_Click(object sender, EventArgs e)
        {
            my_delegate x = new my_delegate(my_function);

            textBox1.Text = x.Invoke(ref num);
         
        }

        private string my_function(ref int x)
        {
            x++;
            return x.ToString();
        }
}
}

--------------------------------------------------------------------------------------------

synchronous thread example



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;

namespace delegate2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        delegate void my_delegate(int x);

        private void button1_Click(object sender, EventArgs e)
        {
            Thread t = new Thread(new ThreadStart(work));
            t.Name = "abc";
            t.Start();
        }

        private void work()
        {
            while(true)
            {
                my_delegate y = new my_delegate(my_function);
                y.Invoke(25);
            }
        }

        private void my_function(int x)
        {
            progressBar1.Value += x;
        }
    }
}

code gives cross thread error

---------------------------------------------------------------------------------------------

fixes

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;

namespace delegate2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        delegate void my_delegate(int x);

        private void button1_Click(object sender, EventArgs e)
        {
            Thread t = new Thread(new ThreadStart(work));
            t.Name = "abc";
            t.Start();
        }

        private void work()
        {
            while(true)
            {
                my_delegate y = new my_delegate(my_function);
                y.Invoke(25);

                if(progressBar1.Value>50)
                {
                    Thread.CurrentThread.Abort();
                }
            }
        }

        private void my_function(int x)
        {
            if(InvokeRequired)
            {
                Invoke(new my_delegate(my_function), x);
            }
            else
            {
                progressBar1.Value += x;
            }
        }
    }
}


first time call my_function


step into code, thread is on my_delegate



call my_function again, thread is on form1



skip the invoke for the second call




when done my_function, thread goes back to my_delegate



---------------------------------------------------------------------------

async delegate
used for long process

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;

namespace delegate3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        delegate bool my_delegate();

        delegate void my_delegate_2();

        private void button1_Click(object sender, EventArgs e)
        {
            my_delegate x = new my_delegate(my_function);
            x.BeginInvoke(new AsyncCallback(end_process), x);
        }

        private void end_process(IAsyncResult result)
        {
            my_delegate y = (my_delegate)result.AsyncState;
            bool done = y.EndInvoke(result);

            if(done)
            {
                my_delegate_2 z = new my_delegate_2(my_function_2);
                z.Invoke();
            }
        }

        private void my_function_2()
        {
            MessageBox.Show("done");
        }

        private bool my_function()
        {
            Thread.Sleep(1000);

            return true;
        }
    }
}

No comments:

Post a Comment