刚开始学C#的一个问题

.Net技术 码拜 8年前 (2016-05-19) 981次浏览
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。

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明刚开始学C#的一个问题
喜欢 (0)
[1034331897@qq.com]
分享 (0)