0

所以我有这段代码可以运行一个工作线程等等。我缩短了它以仅显示必要的部分。在 UploaderDoWork 我有一个我想要 toolStripStatusLabel1 的文本。如何从那里将值分配给 toolStripStatusLabel1?

using Google.GData.Client;
using Google.GData.Extensions.MediaRss;
using Google.GData.Extensions;
using Google.GData.YouTube;
using Google.YouTube;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Security.Cryptography.X509Certificates;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using System.Windows;
using System;
using System.Globalization;
using System.Resources;
using System.Reflection;
using System.ComponentModel;

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

        private static BackgroundWorker uploader;

        static void UploaderDoWork(object sender, DoWorkEventArgs e)
        {
            //How do I assign a value to toolStripStatusLabel1, that is in Form1 from here?
        }

        private void w()
        {
            uploader = new BackgroundWorker { WorkerReportsProgress = true, WorkerSupportsCancellation = true };
            uploader.DoWork += UploaderDoWork;
            uploader.RunWorkerCompleted += delegate { System.Windows.Forms.MessageBox.Show(ursache + " beendet!"); };
            uploader.RunWorkerAsync();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            w();
        }
    }
}
4

1 回答 1

0

您将使用 Control.Invoke 或 Control.BeginInvoke 方法。看看这里http://msdn.microsoft.com/en-us/library/757y83z4(v=vs.71).aspx

像这样的东西:

toolStripStatusLabel.Invoke((MethodInvoker)delegate
        {
            toolStripStatusLabel.Text = "foo";
        });

请注意,这是未经测试的。

于 2012-05-26T12:51:23.630 回答