如何为Form1中的toolStripStatusLabel1赋值?

时间:2012-05-26 12:46:15

标签: c#-4.0

所以我有这个代码运行一个workerthread和诸如此类的东西。我缩短它只显示必要的部分。在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();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

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

这样的事情:

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

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