您现在的位置: 爱51代码网 >> 范文 >> 文章正文
.net线程的事件没有执行

WebBrowser w1; private void button1_Click(object sender, EventArgs e)         {             Thread t = new Thread(p =>             {                 w1 = new WebBrowser();                 w1.ObjectForScripting = this;                 w1.Navigate(System.Environment.CurrentDirectory + @"\test.html");                 w1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(GetBitmap);             });             t.TrySetApartmentState(ApartmentState.STA);             t.Start();         } private void GetBitmap(object sender,WebBrowserDocumentCompletedEventArgs e)         {             HtmlElement element = w1.Document.Body;             int w = element.ScrollRectangle.Width + element.OffsetRectangle.Width;             int h = element.ScrollRectangle.Height + element.OffsetRectangle.Height;               w1.Width = w;             w1.Height = h;             using (Bitmap bmp = new Bitmap(w, h))             {                 w1.DrawToBitmap(bmp, element.ClientRectangle);                 string fileName = "d:\\" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".bmp";                 bmp.Save(fileName);             }         }


问题:运行时没进GetBitmap方法,刚开始接触线程,求详细指点..
PS:不用线程,是能进GetBitmap方法的
异步事件完成,应该是异步还没有执行完,线程就结束了,
所以你可以在
 w1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(GetBitmap);
这句后面,设置一个信号量,让线程不要结束,然后在
private void GetBitmap(object sender,WebBrowserDocumentCompletedEventArgs e)
方法里,重置这个信号量。
 WebBrowser w1; AutoResetEvent autoResetEvent = new AutoResetEvent(false); private void button1_Click(object sender, EventArgs e)         {             Thread t = new Thread(p =>             {                 w1 = new WebBrowser();                 w1.ObjectForScripting = this;                 w1.Navigate(System.Environment.CurrentDirectory + @"\test.html");                 w1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(GetBitmap);                 autoResetEvent .WaitOne();             });             t.TrySetApartmentState(ApartmentState.STA);             t.Start();         } private void GetBitmap(object sender,WebBrowserDocumentCompletedEventArgs e)         {             HtmlElement element = w1.Document.Body;             int w = element.ScrollRectangle.Width + element.OffsetRectangle.Width;             int h = element.ScrollRectangle.Height + element.OffsetRectangle.Height;                w1.Width = w;             w1.Height = h;             using (Bitmap bmp = new Bitmap(w, h))             {                 w1.DrawToBitmap(bmp, element.ClientRectangle);                 string fileName = "d:\\" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".bmp";                 bmp.Save(fileName);             }                //即通知正在等待的线程有事件发生                   autoResetEvent.Set();           }
WebBrowser w1; AutoResetEvent autoResetEvent = new AutoResetEvent(false); private void button1_Click(object sender, EventArgs e)         {             Thread t = new Thread(p =>             {                 w1 = new WebBrowser();                 w1.ObjectForScripting = this;                 w1.Navigate(System.Environment.CurrentDirectory + @"\test.html");                 w1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(GetBitmap);                 while (w1 .ReadyState != WebBrowserReadyState.Complete)                 {                        Application.DoEvents();                 }             });             t.TrySetApartmentState(ApartmentState.STA);             t.Start();             t.Join();         } private void GetBitmap(object sender,WebBrowserDocumentCompletedEventArgs e)         {             HtmlElement element = w1.Document.Body;             int w = element.ScrollRectangle.Width + element.OffsetRectangle.Width;             int h = element.ScrollRectangle.Height + element.OffsetRectangle.Height;                 w1.Width = w;             w1.Height = h;             using (Bitmap bmp = new Bitmap(w, h))             {                 w1.DrawToBitmap(bmp, element.ClientRectangle);                 string fileName = "d:\\" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".bmp";                 bmp.Save(fileName);             }                //即通知正在等待的线程有事件发生                   autoResetEvent.Set();           }

  • 上一篇文章:

  • 下一篇文章: 没有了
  • 最新文章 热点文章 相关文章
    redhat 2.6 (santigo 5.6) vsftp
    shell如何实现自动填写操作执行下
    linux shell 文件配置sh:color:
    shell script语法一定要加path吗
    SecureCRT如何访问虚拟机vmWare中
    C#如何读取WINDOWS的放大系数
    cximge的图如何存入数据库并提取
    DBGRID控件显示查询结果文本类型
    TChart控件如何把表中右边的系列
    fastreport提示没有找到fr_class
    linux下编译faac及faad2提示 rec
    TClientDataSet如何避免多人更新
    如何用delphi代码修改XML节点
    vc++ pvoid *用delphi怎么表达
    GIS用百度行政区域的经纬度直接画
    不小心卸载了.net 4 extend,vs2
    C#byte【】里的数转换成string时
    C#的session取不到值
    java如何判断一个字符串里的数字
    随机找出24个不一样的字,在把一
    没有相关文章
     



    设为首页 | 加入收藏 | 网站地图 | 友情链接 |