using System; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsFormsApplication7 { public partial class Form1 : Form { private static Form form; public Form1() { InitializeComponent(); } public static void StartTask(Action action, Action onFinish = null) { var task = new Task(() => { action(); if (onFinish != null) { form.BeginInvoke(new FinishDelegele(onFinish)); } }); task.Start(); } public delegate void FinishDelegele(); private void button1_Click(object sender, EventArgs e) { bool s = false; StartTask(() => { HttpHelper.Get("http://www.hao123.com"); }, () => { HttpHelper.Get("http://www.baidu.com"); }); } } }
本人的想法是等访问完hao123后再访问百度,但是程序运行后只会访问hao123,不会访问百度,这是为什么呢?
解决方案
20
你的form静态字段没赋值,为null。