0

如何在 C# 中将RichTextBox1' 值扔给 a ?BackgroundWorker

public void button4_Click(object sender, EventArgs e)
{
    if (String.IsNullOrEmpty(richTextBox1.Text.Trim())){
        MessageBox.Show("No value in RichTextBox?");
        return;
    }

    if (backgroundWorker1.IsBusy != true)
    {
        // Start the asynchronous operation.
        backgroundWorker1.RunWorkerAsync(richTextBox1);
    }

这是我BackgroundWorker的代码:

public void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) {
    HtmlAgilityPack.HtmlDocument document = new HtmlAgilityPack.HtmlDocument();
    foreach (string vr in richTextBox1.Lines)
    {
    ⋮
    }
}
4

3 回答 3

2

由于上述原因,您不能传递 RichTextBox,但您可以传递作为 RichTextBox.Lines 属性的字符串数组并对其进行迭代。

private void button1_Click(object sender, EventArgs e)
{
    bg.RunWorkerAsync(richTextBox1.Lines);
}

void bg_DoWork(object sender, DoWorkEventArgs e)
{
    string[] lines = (string[])e.Argument;
    foreach(string vr in lines)
    {

    }
}
于 2012-12-24T06:20:54.933 回答
1

通常只有 Main UI 线程可以与 Forms 和 Controls 交互。

考虑将它需要的所有数据传递给该RunWorkerAsync方法——也许RunWorkerAsync(richTextBox1.Lines.ToList())

于 2012-12-24T02:56:39.467 回答
1
private void button1_Click(object sender, EventArgs e)
    {
        backgroundWorker1.RunWorkerAsync(richTextBox1.Text);

    }

    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
        string text = (string)e.Argument;
        MessageBox.Show(text);

    }

文本作为对象 e.argument 发送。要检索它,请将 e.argument 转换回字符串(或 string[] 等,具体取决于您传递的内容)

于 2012-12-24T06:00:33.300 回答