0

我想从互联网上读取一个文本文件,我想在读取文件时显示一个图片框,即 gif 动画,显示并在读取完成后隐藏图片框。我使用后台工作者。我有一个显示状态的标签,但是当我单击BtnCheck按钮时,bg 不起作用并且标签没有改变。

我的代码:

    private void Form1_Load(object sender, EventArgs e)
    {
        backgroundWorker1.WorkerReportsProgress = true;
        backgroundWorker1.WorkerSupportsCancellation = true;
    }
    private void BtnCheck_Click(object sender, EventArgs e)
    {
        PbLoading.Visible = true;
        if (backgroundWorker1.IsBusy != true)
        {
            // Start the asynchronous operation.
            backgroundWorker1.RunWorkerAsync();
        }
    }
    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {

        LbleState.Text = "Reading txt File...";
        webClient1 = new WebClient();
        if (CheckForInternetConnection())
        {
            try
            {
                Stream stream = webClient1.OpenRead(TxtWebAdrss);
                StreamReader reader = new StreamReader(stream);
                String content = reader.ReadToEnd();
                reader.Close();
                LbleState.Text = "Reading Finished .";
            }
            catch
            {
                LbleState.Text = "Error reading";
            }


        }
        else LbleState.Text = "Internet not connected!";

    }
4

1 回答 1

0

你可能只需要对这门课做更多的研究。您应该在 UI 线程上执行 UI 更改。

您可以使用三个事件处理程序,它们是,

backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)

以下链接应该有帮助,

http://msdn.microsoft.com/en-us/library/System.ComponentModel.BackgroundWorker(v=vs.110).aspx

于 2013-12-08T16:31:01.110 回答