Task Usage

Task of Use

1. Call no parameters and returns no value method

private void button1_Click(object sender, EventArgs e)
{
Task task = new Task(() => { A(); });

task.Start ();
}

/// <Summary>
/// None None Return Value parameter method
/// </ Summary>
public void A ()
{

}

2. The call returns a value, non-parametric methods

private void button1_Click(object sender, EventArgs e)
{
Task<bool> task = new Task<bool>(() => { return A(); });
task.Start();

// Returns a value obtained results
bool result = task.Result;

MessageBox.Show (result.ToString ());
}

/// <Summary>
///  return value, a method without parameters
/// </ Summary>
public BOOL A ()
{
return to true;
}

3. Call no return value, there are parameters

void the button1_Click Private (SENDER Object, EventArgs E)
{
String Parameter = "parameter";
the Task Task the Task new new = (() => {A (Parameter);});
task.Start ();
}

/// <Summary>
/// no return value, a method has parameters
/// </ Summary>
public void A (String STR)
{
   // method member
}

4. The call returns a value, the method has parameters

private void button1_Click(object sender, EventArgs e)
{
string parameter = "参数";
Task<bool> task = new Task<bool>(() => { return A(parameter); });
task.Start();

// Get results
bool result = task.Result;

MessageBox.Show (result.ToString ());
}

/// <Summary>
///  return value, a method has parameters
/// </ Summary>
public BOOL A (String STR)
{
return to true;
}

 

Guess you like

Origin www.cnblogs.com/yuanshuo/p/11514263.html