Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.8k views
in Technique[技术] by (71.8m points)

我写一个开启N多个task子任务的传参方法,但结果不对,求教

我的测试环境是基于.net 4.5测试的,但发现有问题,请大神们帮忙指导一下,谢谢:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 测试开启N多个子task任务并实现将i作为参数传入,但是结果不对头呢,这个怎么应该怎么处理啊?
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            Task<string[]> parent = new Task<string[]>(state => {
                Console.WriteLine(state); 
                string[] result = new string[100000];
                for (int i = 0; i < result.Length; i++)
                {
                    new Task(() => { result[i] = "我是子任务::"+i.ToString(); Console.WriteLine(result[i]); }, TaskCreationOptions.AttachedToParent).Start();
                }
                //创建并启动子任务                
                //new Task(() => { result[1] = "我是子任务2。"; }, TaskCreationOptions.AttachedToParent).Start(); 
                return result;

            }, "我是父任务,并在我的处理过程中创建多个子任务,所有子任务完成以后我才会结束执行。");//任务处理完成后执行的操作

            parent.ContinueWith(t => {
                //Array.ForEach(t.Result, r => Console.WriteLine(r));

            });//启动父任务

            parent.Start();//等待任务结束 Wait只能等待父线程结束,没办法等到父线程的ContinueWith结束//parent.Wait();

            Console.ReadLine();
        }
    }
}

运行结果如下:
image
发现这个传参的有问题,我想把i这个参数传入进去执行,这个问题应该怎么解决呀?求指导,谢谢!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

要用闭包呀。

Task<string[]> parent = new Task<string[]>(state => {
    Console.WriteLine(state); 
    string[] result = new string[100000];
    for (int i = 0; i < result.Length; i++)
    {
        // 【这里改了】
        new Task((state) => { int i = (int)state; result[i] = "我是子任务::"+i.ToString(); Console.WriteLine(result[i]); }, i, TaskCreationOptions.AttachedToParent).Start();
    }
    //创建并启动子任务 
    //new Task(() => { result[1] = "我是子任务2。"; }, TaskCreationOptions.AttachedToParent).Start(); 
    return result;

}, "我是父任务,并在我的处理过程中创建多个子任务,所有子任务完成以后我才会结束执行。");//任务处理完成后执行的操作 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...