简体   繁体   English

如何在C#线程中读取Cookie文件

[英]How to read cookie files in c# thread

I have this function for reading cookie files in C#: 我具有用于读取C#中的cookie文件的此功能:

private void getdocument()
{
 while (true)
 {
 MessageBox.Show(webBrowser1.Document.Cookie);
 }
}

I am invoking it like this: 我这样调用它:

private void Form1_Load(object sender, EventArgs e)
{
 Thread MyThread = new Thread(new ThreadStart(getdocument));
 MyThread.Start();
}

But I'm getting an error: 但是我遇到一个错误:

System.InvalidCastException was unhandled 未处理System.InvalidCastException

Specified cast is not valid. 指定的演员表无效。

How to fix this? 如何解决这个问题?

在此处输入图片说明

As you can see in Object Hierarchy structure WebBrowser is inherited from System.Windows.Forms.Control , so you need to you it from the thread it was created on. 正如您在“对象层次结构”中看到的那样, WebBrowser结构继承自System.Windows.Forms.Control ,因此您需要从创建它的线程中使用它。

Fix : 解决:

private void getdocument()
{
    while (true)
    {

        this.Invoke((MethodInvoker) delegate
            {
                MessageBox.Show(webBrowser1.Document.Cookie);
            });
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM