以文本方式查看主题 - Foxtable(狐表) (http://foxtable.net/bbs/index.asp) -- 专家坐堂 (http://foxtable.net/bbs/list.asp?boardid=2) ---- webbrowser操作网页,内存问题 (http://foxtable.net/bbs/dispbbs.asp?boardid=2&id=76755) |
-- 作者:ycli368 -- 发布时间:2015/11/4 15:58:00 -- webbrowser操作网页,内存问题 webbrowser操作网页,没操作一个网页就new,操作完和操作失败均为 dispose,但是内存还是飙升的很厉害,求解 |
-- 作者:大红袍 -- 发布时间:2015/11/4 16:01:00 -- 贴出可测试的代码,不应该的。 |
-- 作者:ycli368 -- 发布时间:2015/11/4 16:01:00 -- Dim Daima As String Dim CheckedRowsYhq As List(of Row) Dim AllRowsYhq As List(of Row) Dim CheckedRowsBb As List(of Row) Dim YhqArgs(20) As String Dim AllYhqArgs(40) As String Dim ReturnFlag As Integer = 0 Dim ErrStr As String \'Dim web As New system.windows.Forms.WebBrowser \'web.ScriptErrorsSuppressed = True Dim elements,element, elements2,element2, elements3,element3, elements4,element4, elements5,element5 As object Dim elements6,element6,elements7,element7,elements8,element8 As object FileSys.WriteAllText("result.txt","", False, Encoding.Default) Dim DaimaYuan As DataRow = DataTables("代码信息").sqlfind("代码类型 = \'优惠券\'") CheckedRowsyhq = Tables("优惠券信息").GetCheckedRows If CheckedRowsyhq.count = 0 Then messagebox.show("请选择要设置的优惠券!") \'web.Dispose Return 1 End If CheckedRowsBb = Tables("宝贝信息").GetCheckedRows \'messagebox.show(CheckedRowsbb.count) If CheckedRowsbb.count = 0 Then messagebox.show("请选择要设置的宝贝!") \'web.Dispose Return 1 End If messagebox.show("开始设置优惠券,请暂时不要操作宝贝!") \'AllRowsYhq = Tables("优惠券信息").Rows For y As Integer = 0 To Tables("优惠券信息").Rows.count - 1 AllYhqArgs(y) = DaimaYuan("代码1") & Tables("优惠券信息").Rows(y)("卖家ID") & DaimaYuan("代码2") & Tables("优惠券信息").Rows(y)("优惠券标示") & DaimaYuan("代码3") Next For i As Integer = 0 To CheckedRowsyhq.count - 1 YhqArgs(i) = DaimaYuan("代码1") & CheckedRowsyhq(i)("卖家ID") & DaimaYuan("代码2") & CheckedRowsyhq(i)("优惠券标示") & DaimaYuan("代码3") Daima = Daima & YhqArgs(i) Next \'开始设置宝贝 For j As Integer = 0 To CheckedRowsBb.count - 1 Dim web As New system.windows.Forms.WebBrowser web.ScriptErrorsSuppressed = True try \'页面加载完整,从两方面判断 \'判断页面是否初步加载完成 web.Navigate(CheckedRowsBb(j)("宝贝修改链接")) \'messagebox.show(CheckedRowsBb(j)("宝贝修改链接")) Do While web.Isbusy OrElse (web.ReadyState <> 4) OrElse (web.Document Is Nothing) Yanshi(100)\'让渡下否则读不到数据 If ReturnFlag = 300 Then \'messagebox.show("页面处理异常,请检查网络或账号登陆1") web.dispose ErrStr = CheckedRowsBb(j)("宝贝ID") & chr(9) & "Fail" & chr(9) & "页面处理异常,请检查网络或账号登陆1" & chr(10) FileSys.WriteAllText("result.txt",ErrStr, True, Encoding.Default) \'web.Document.window.close Continue For ElseIf ReturnFlag Mod 10 = 0 Then web.Navigate(CheckedRowsBb(j)("宝贝修改链接")) End If ReturnFlag = ReturnFlag + 1 Loop ReturnFlag = 0 \'FileSys.WriteAllText("C:\\Users\\guigu\\Documents\\Foxtable项目\\派券精灵\\1.txt", web.Document.body.outerhtml, False, Encoding.Default) \'判断页面是否加载完整 Do until (web.Document.body.outerhtml.contains("电脑端") ) Yanshi(100)\'让渡下否则读不到数据 If ReturnFlag = 100 Then \'messagebox.show("页面处理异常,请检查网络或账号登陆2") web.dispose ErrStr = CheckedRowsBb(j)("宝贝ID") & chr(9) & "Fail" & chr(9) & "页面处理异常,请检查网络或账号登陆1" & chr(10) FileSys.WriteAllText("result.txt",ErrStr, True, Encoding.Default) \'web.Document.window.close Continue For End If ReturnFlag = ReturnFlag + 1 Loop ReturnFlag = 0 \'messagebox.show(2) elements = web.document.GetElementById("J_TabWPDesc").GetElementsByTagName("li") \'来到电脑端详情页页面 For Each element In elements If element.outerhtml.contains("电脑端") Then element.InvokeMember("click") yanshi(600) Exit For End If Next \'messagebox.show(3) \'切换到代码模式 element2 = web.document.GetElementById("keTplMod_1") \'.GetElementsByTagName("li") \'如果第一个模块没有打开,则单击打开 If not(element2.outerhtml.contains("desc-tpl-item desc-tpl-category desc-tpl-endup desc-tpl-hascontent")) Then elements3 = element2.GetElementsByTagName("a") For Each element3 In elements3 If element3.outerhtml.contains("编辑") Then element3.InvokeMember("click") yanshi(600) Exit For End If Next End If \'messagebox.show(4) \'单击切换到编码模式 element4 = web.document.GetElementById("newTabContent") elements5 = element4.GetElementsByTagName("span") For Each element5 In elements5 If element5.outerhtml.contains("ks-editor-toolbar-item ks-editor-toolbar-sourceArea") Then element5.InvokeMember("click") yanshi(600) Exit For End If Next \'完成代码替换 \'elements6 = element4.GetElementsByTagName("textarea") \'messagebox.show(elements6(0).Innertext) \'FileSys.WriteAllText("C:\\Users\\guigu\\Documents\\Foxtable项目\\派券精灵\\1.txt",elements6(0).Innertext, False, Encoding.Default) \'Dim Strtmp As String = elements6(0).Innertext \'For m As Integer = 0 To CheckedRowsyhq.count - 1 \' Strtmp = Strtmp.replace(YhqArgs(m),"") \'Next \'elements6(0).Innertext = Daima & Strtmp \'FileSys.WriteAllText("C:\\Users\\guigu\\Documents\\Foxtable项目\\派券精灵\\1.txt",elements6(0).Innertext, False, Encoding.Default) \'messagebox.show(web.Document.window.frames.count) \'For s As Integer = 0 To web.Document.window.frames.count \'messagebox.show(web.Document.window.frames(s).document.body.outerhtml) \'Next elements6 = web.Document.window.frames(1).document.GetElementsByTagName("body") \'messagebox.show(elements6(0).outerhtml) Dim Strtmp As String = elements6(0).outerhtml For m As Integer = 0 To Tables("优惠券信息").Rows.count - 1 Strtmp = Strtmp.replace(AllYhqArgs(m),"") \'messagebox.show(YhqArgs(m)) \'messagebox.Show(Strtmp.IndexOf(YhqArgs(m).trim(vblf,chr(10),Chr(13)))) Next Dim Tihuan As String = "<body class=" & """" & "ks-editor" & """" & ">" Strtmp.trim(Tihuan) elements6(0).outerhtml = Tihuan & Daima & Strtmp \'FileSys.WriteAllText("C:\\Users\\guigu\\Documents\\Foxtable项目\\派券精灵\\1.txt",elements6(0).outerhtml, False, Encoding.Default) \'单击切换到图片模式 element4 = web.document.GetElementById("newTabContent") elements5 = element4.GetElementsByTagName("span") For Each element5 In elements5 If element5.outerhtml.contains("ks-editor-toolbar-item ks-editor-toolbar-sourceArea") Then element5.InvokeMember("click") yanshi(600) Exit For End If Next \'提交修改 element6 = web.Document.GetElementById("event_submit_do_edit") element6.InvokeMember("click") Do until (web.Document.url.Tostring.contains("item.htm?") ) Yanshi(100)\'让渡下否则读不到数据 If ReturnFlag = 600 Then \'messagebox.show("页面处理异常,请检查网络或账号登陆3") ErrStr = CheckedRowsBb(j)("宝贝ID") & chr(9) & "Fail" & chr(9) & web.Document.url.Tostring & chr(10) FileSys.WriteAllText("result.txt",ErrStr, True, Encoding.Default) web.dispose \'web.Document.window.close Continue For ElseIf ReturnFlag Mod 10 = 0 Then element6.InvokeMember("click") End If ReturnFlag = ReturnFlag + 1 Loop ReturnFlag = 0 \'messagebox.show(web.Document.url.Tostring) Catch ex As Exception web.dispose ErrStr = CheckedRowsBb(j)("宝贝ID") & chr(9) & "Fail" & chr(9) & ex.tostring & chr(10) FileSys.WriteAllText("result.txt",ErrStr, True, Encoding.Default) \'web.Document.window.close Continue For End try Dim ErrStrsuccess As String = CheckedRowsBb(j)("宝贝ID") & chr(9) & "Success" & chr(10) FileSys.WriteAllText("result.txt",ErrStrsuccess, True, Encoding.Default) web.dispose \'web.Document.window.close Next \'web.Dispose messagebox.show("修改成功")
|
-- 作者:大红袍 -- 发布时间:2015/11/4 16:11:00 -- 看了一下代码,内存不应该一直上升才对的,会稳定在一个最大值才对,也就是比如飙到100后,接着跌回50,再飙到100,但不会超过100这样。
但是你用了 do until,那么cpu就会占用得比较多的。 |
-- 作者:ycli368 -- 发布时间:2015/11/4 16:19:00 -- 这个最大值会一直上升,也就处理几十个页面,内存0.6G |
-- 作者:大红袍 -- 发布时间:2015/11/4 16:21:00 -- 做个可以测试的例子瞧瞧呗,dispose应该是会释放空间的了。 |
-- 作者:ycli368 -- 发布时间:2015/11/4 16:23:00 -- 测试有点难,这个是操作淘宝宝贝页面,搞不出测试页面 |
-- 作者:ycli368 -- 发布时间:2015/11/4 16:23:00 -- 回复期间,已经飙到1个G了 |
-- 作者:大红袍 -- 发布时间:2015/11/4 16:24:00 -- 以下是引用ycli368在2015/11/4 16:23:00的发言:
测试有点难,这个是操作淘宝宝贝页面,搞不出测试页面
换一个网址,写一段简单代码测试一下。 |
-- 作者:ycli368 -- 发布时间:2015/11/4 16:25:00 -- 袍大大,我的延时函数没问题吧 \'****延时函数,单位是毫秒,支持小数**** Public Function YanShi(HaoMiao As Double) Dim t1 t1 = timeGetTime While (timeGetTime - t1) < HaoMiao Application.DoEvents End While End Function |