如何在Windows应用程序中的asyanc TASK中为文本框添加值

时间:2016-12-28 10:31:01

标签: c# winforms windows-applications

您好我是新手赢得Form,我想在表单上的文本框中显示以下任务的进度,但我无法这样做。

 // Create progress printing and querying tasks
    Task progressPrintTask = new Task(() =>
    {

        IJob jobQuery = null;
        do
        {
            var progressContext = new CloudMediaContext(_accountName, _accountKey);
            jobQuery = progressContext.Jobs.Where(j => j.Id == job.Id).First();
           // AppendTextBox("Indexer Job started");
           //  textBox2.AppendText(string.Format("{0}\t{1}\t{2}", DateTime.Now, jobQuery.State, jobQuery.Tasks[0].Progress));
            //  Console.WriteLine(string.Format("{0}\t{1}\t{2}", DateTime.Now, jobQuery.State, jobQuery.Tasks[0].Progress));
            Thread.Sleep(10000);
        }
        while (jobQuery.State != JobState.Finished &&
               jobQuery.State != JobState.Error &&
               jobQuery.State != JobState.Canceled);
    });

    progressPrintTask.Start();

请检查屏幕截图更多细节

enter image description here

请帮忙

1 个答案:

答案 0 :(得分:0)

我猜你错过了一个调用:

// Create progress printing and querying tasks
Task progressPrintTask = new Task(() =>
    {

        IJob jobQuery = null;
        do
        {
            var progressContext = new CloudMediaContext(_accountName, _accountKey);
            jobQuery = progressContext.Jobs.Where(j => j.Id == job.Id).First();
            textBox2.Invoke(() => textBox2.AppendText(string.Format("{0}\t{1}\t{2}", DateTime.Now, jobQuery.State, jobQuery.Tasks[0].Progress)));
            Thread.Sleep(10000);
        }
        while (jobQuery.State != JobState.Finished &&
               jobQuery.State != JobState.Error &&
               jobQuery.State != JobState.Canceled);
    });
progressPrintTask.Start();