JANA GANA MANA

Promoting indian languages
search BLOGS
Login
Add your comments
Want to contribute?
Join JANAGANAMANA



Print this page.




Threading in C#

- ASP.NET

pokiri(8/24/2008)
//Add a delegate
public delegate int binaryop(int a, int b);

private int add(int a, int b)
{
    Thread.Sleep(5000);
    MessageBox.Show("a Done");
    return a + b;
}
private void addDone(IAsyncResult rr)
{
    MessageBox.Show("ADDDONE");
}

Now try 
main()
{

binaryop b = new binaryop(add);
int X = b(10, 10);
IAsyncResult res = b.BeginInvoke(10, 10, null, null); 

MessageBox.Show("m Done");
int Y = b.EndInvoke(res);

IAsyncResult res2 = b.BeginInvoke(10, 10, addDone, null); 

MessageBox.Show("m Done");
int Z = b.EndInvoke(res2);
}

pokiri(8/24/2008)
Other ways of starting threads is 

Thread r = new Thread( new ParameterizedThreadStart(iteratenumbers));
            r.Start(10000 );
Thread s = new Thread(new ParameterizedThreadStart(iteratenumbers));
            s.Start(100000);



Where
int globalI = 0;
private void iteratenumbers(object r)
{
    Monitor.Enter(this);
    int x = (Int32)r;int z = 0;
    for (; globalI < x; globalI++)
    {
        lock (this)
        {
            z++;
        }
    }
    MessageBox.Show(z.ToString());
    Monitor.Exit(this);
}
pokiri(8/24/2008)
Thread synchronization in next topic....