本文介绍了同步线程的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这里,我陷入了线程问题.

我创建了一个单击网页上按钮的功能,该功能会启动下载对话框(在浏览器控件内部).我在IE和下载"对话框中遇到了一个最令人沮丧的问题.似乎实际的下载文件"对话框是由Web浏览器在新线程中启动的.通常这很好,但事实证明,为下载而启动的新线程是同步启动的,因此,直到单击下载对话框上的按钮或对话框关闭(完成或取消)后,当前线程才会继续.

当我使用此代码时,主线程会暂停,直到新创建的线程停止为止.新线程尝试查找对话框窗口,该窗口将失败并停止.在此主线程恢复并显示对话框之后.

我无法同步此线程,不胜感激的任何帮助

Here I''m stuck in a problem with threading.

I have created one function which clicks button on web page which launches the download dialog box (inside browser control). I ran into a most frustrating issue with IE and the Download dialog. It seems that the actual "Download File" dialog is launched in a new thread by the web browser. While this would normally be fine, it turns out that the new thread launched for the the download is launched synchronously, so the current thread will not continue until a button on the download dialog is clicked, or the dialog closes (finished or cancelled).

When I use this code the main thread halts till the newly created threads stop. the new thread tries to find dialog window which is where it fails and it stops. After this main thread resumes and the dialog box appears.

I''m not able to synchronize this threads, any help greatly appreciated

public void DownloadFile()
        {
            HtmlElementCollection links = this.webBrowser1.Document.GetElementsByTagName("input");
            int btn = links.Count - 1;
            //links[btn].InvokeMember("Click"); 
            object btn1 = links[btn].DomElement;
            btn1.GetType().InvokeMember("click",System.Reflection.BindingFlags.InvokeMethod, null, btn1, null);
            Thread workThread = new Thread(delegate() { FindDownloadDialogWindow("#32770", "File Download", 25, @"D:\abc.zip"); });
            //wait until the dialog is found and file saved.
            workThread.Start();
            while (workThread.ThreadState != System.Threading.ThreadState.Stopped)
            {
                Thread.Sleep(1000);
                Application.DoEvents();
            }
        }

推荐答案


//This Event _DoWork Occurs on separate thread
        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            try
            {
                 //implemen your download logic here

                 backgroundWorker1.ReportProgress(1);   //this is to report progress of your work
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message,"Download File");
            }
        }

// here you can access controls of main form or thread
        private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            //you might want to show the download progres
            progressBar1.Increment(e.ProgressPercentage);
        }

// this event occours once your job is finished
        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (checkBox_close.Checked)
            {
                this.Close();
                this.Dispose();
            }
            label_size3.Text += " COMPLETED";
        }

//the Do work event is called from main thread or form like this

            backgroundWorker1.RunWorkerAsync();



这篇关于同步线程的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 19:18