using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace _08_2_异步
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private delegate void Dome(string name);
private void but_ays_Click(object sender, EventArgs e)
{
Console.WriteLine("=================异步========================");
for (int i = 0; i < 5; i++)
{
string name = string.Format("id = {0}",i.ToString());
Dome method = Something;
method.BeginInvoke(name,null,null);
}
Console.WriteLine("=================异步结束====================");
}
//耗时方法
static void Something(string name)
{
Stopwatch watch = new Stopwatch();
watch.Start();
Console.WriteLine("这里name = {0} ,当前线程id = {1}",name,Thread.CurrentThread.ManagedThreadId);
long result = 0;
for (int i = 0; i < 100000000; i++)
{
result += i;
}
Thread.Sleep(1000);
watch.Stop();
Console.WriteLine("这里name = {0} ,当前线程id = {1} 计算结果 = {2} 耗时 = {3}",name,Thread.CurrentThread.ManagedThreadId,result,watch.ElapsedMilliseconds);
}
private void but_sync_Click(object sender, EventArgs e)
{
Console.WriteLine("=================同步========================");
for (int i = 0; i < 5; i++)
{
string name = string.Format("id = {0}", i.ToString());
Something(name);
//Dome method = Something;
//method.BeginInvoke(name, null, null);
}