You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

52 lines
947 B

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace UseAwait
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private async void RunIt()
{
Task<double> task = Task<double>.Factory.StartNew(() => LongCalc(5));
// not block the UI thread
await task;
progressText.Text = task.Result.ToString();
}
private double LongCalc(double r)
{
Thread.Sleep(3000);
return Math.PI * r * r;
}
private void syncButton_Click(object sender, EventArgs e)
{
Task<double> task = Task<double>.Factory.StartNew(() => LongCalc(5));
task.Wait();
progressText.Text = task.Result.ToString();
}
private void asyncButton_Click(object sender, EventArgs e)
{
RunIt();
}
}
}