[2014-9-13] Commissioned multithreading

Source: Internet
Author: User

Time to study in Saturday.

Continue learning delegate, multithreading.

1. Definition of a simple delegate

delegate int Mydel (int a,int b);

Mydel Mydel = new Mydel (ADD); The Add method must have the same parameters and return values as the delegate Mydel

MYDEL+=ADD2//Multicast delegation

2. Anonymous functions

Mydel del =delegate (int a,int b) {return a+b}

Console.Write (Del (3,1))

3.lamba-expression

Mydel Lamba = (A, b) =>{return a+b}

can be shortened to Mydel lamba= (A, b) =>a+b;

Console.Write (Lamba (3,1));

4 extension methods

Write extension method satisfies: Static class, static method, this

Example code:

static void Main (string[] args)
{
list<string> list = new list<string> () {"1", "2", "3", "4"};
var temp = List.findcalc (MYCALC);//Call to extension method
foreach (var item in temp)
{
Console.WriteLine (item);
}
Console.readkey ();
}
static bool Mycalc (string str)//method is consistent with the delegate type
{
if (int. Parse (str) >= 2)
{
return true;
}
return false;
}

Extension methods:

namespace System.Collections.Generic//note namespaces are written in the list generic collection in a consistent space for easy invocation
{
delegate bool Isokdel<t> (T obj); Defining delegates
Static Class Fun
{
public static list<t> findcalc<t> (this list<t> list,isokdel<t> del)//this keyword, indicated on the List above
{
list<t> result = new list<t> ();
foreach (var item in list)
{
if (Del (item))
{
Result. ADD (item);
}
}
return result;
}
}
}
5 Generic Delegates

Func<int,bool> del = a=> a>2//has return value

Action<> no return value

list<int> myintlst = new List<int> () {1, 2, 3, 4, 5, 6};

Pass a delegate to a method and invoke it inside the method. Determine the return of a set to satisfy a condition
var result = Myintlst.where (DEL);
foreach (var i in result)
{
Console.WriteLine (i);
}

6 Threads with parameters

Thread thread = new Thread (a =//parameter is a delegate
{
while (true)
{
Thread.CurrentThread.ManagedThreadId: The CLR is helping us assign
Console.WriteLine ("This is a sub-thread is working on it.") Parameter value: {0}[email protected]{1} ", A, Thread.CurrentThread.ManagedThreadId);
Thread.Sleep (1000);
}
});
Thread. IsBackground = true;
Thread. Start (2);//Pass Parameters

7 understanding of the previous error

The scheduling of a thread is determined by the operating system, and the priority you set for scheduling is highest and determined by the operating system, not by installing the main thread and then executing other threads.

We believe one thing,today is Difficult,tomorrow are more difficult,but the day after tomorrow are beautiful.

[2014-9-13] Commissioned multithreading

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.