Textbox text from background worker?

后端 未结 4 1639
鱼传尺愫
鱼传尺愫 2021-01-13 14:14

I\'ve been trying to figure out how to get my textbox\'s text or other property from within a background worker. Does anybody know how to do this? I cannot pass it as a para

4条回答
  •  北海茫月
    2021-01-13 14:50

    i think you should use invoke method.

    here's my example.

    delegate void myDelegate(string name);
    //...
    private void writeToTextbox(string fCounter)
    {
        if (this.InvokeRequired)
        {
            myDelegate textWriter = new myDelegate(displayFNums);
            this.Invoke(textWriter, new object[] { fCounter });
        }
        else
        {
            textbox1.Text = "Processing file: " + fileCounter + "of" + 100;
        }
    }
    //...
    
    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
        //...
        writeToTextbox(fileCounter.ToString());
    }
    

    in dowork i manipulate some textfile and i inform the user about how many files i have processed so far.

提交回复
热议问题