以文本方式查看主题

-  Foxtable(狐表)  (http://www.foxtable.com/bbs/index.asp)
--  专家坐堂  (http://www.foxtable.com/bbs/list.asp?boardid=2)
----  [求助]能不能多线程检索  (http://www.foxtable.com/bbs/dispbbs.asp?boardid=2&id=117058)

--  作者:天一生水
--  发布时间:2018/4/4 21:35:00
--  [求助]能不能多线程检索
论坛中的检索文档内容字符串,我测试了一下,基本检索一个文件用时1-2秒,比较耗时,能否改进加快:
http://www.foxtable.com/bbs/dispbbs.asp?boardid=2&id=70797&authorid=0&page=0&star=2

mark word高亮查找

 

Dim app As New MSWord.Application
try
    Dim dlg As New FolderBrowserDialog
    If dlg.ShowDialog = DialogResult.Ok Then
        MessageBox.Show("需要检索的文档目录是:" & dlg.SelectedPath,"提示")
    End If
    Dim s As String = ""
    For Each file As String In FileSys.GetFiles(dlg.SelectedPath)
        If file.ToLower.EndsWith(".doc") Then
            Dim doc = app.Documents.Open(file)
            Dim gjzs As String = "Test|Abcdefg|test"
            Dim flag As Boolean = False
            For Each gjz As String In gjzs.split("|")
                app.ActiveWindow.Selection.WholeStory
                With app.ActiveWindow.Selection.Find
                    .HitHighlight(gjz, RGB(255, 255, 0), , , , , , , True)
                End With
                If app.ActiveWindow.Selection.Find.Execute Then flag = True
            Next
            If flag = False  Then
                Doc.close
            Else
                s = s & IIF(s>"", chr(13), "") & file
            End If
        End If
    Next
    If s > "" Then
        MessageBox.Show("共计找到包含关键字的文档有" & s.Split(chr(13)).Length & "个,目录如下:" & vbcrlf & s,"关键字文档查找",MessageBoxButtons.OK,MessageBoxIcon.None)
        app.Visible = True
    Else
        MessageBox.Show("没有找到包含关键字的文档","关键字文档查找",MessageBoxButtons.OK,MessageBoxIcon.None)
        app.quit
    End If
    
catch ex As exception
    msgbox(ex.message)
    app.Quit
finally
End try


--  作者:有点甜
--  发布时间:2018/4/4 22:04:00
--  

全局代码

 

Public Sub thread_sub1(ByVal file As object)
Dim app As New MSWord.Application
try
    Dim doc = app.Documents.Open(file)
    Dim gjzs As String = "Test|Abcdefg|test"
    Dim flag As Boolean = False
    For Each gjz As String In gjzs.split("|")
        app.ActiveWindow.Selection.WholeStory
        With app.ActiveWindow.Selection.Find
            .HitHighlight(gjz, RGB(255, 255, 0), , , , , , , True)
        End With
        If app.ActiveWindow.Selection.Find.Execute Then flag = True
    Next
    If flag = False  Then
        Doc.close
        app.quit
    Else
        app.Visible = True
    End If
catch ex As exception
    msgbox(ex.message)
    app.Quit
finally
   
End try

End Sub

 

调用代码

 

Dim dlg As New FolderBrowserDialog
If dlg.ShowDialog = DialogResult.Ok Then
    MessageBox.Show("需要检索的文档目录是:" & dlg.SelectedPath,"提示")
End If
Dim s As String = ""
For Each file As String In FileSys.GetFiles(dlg.SelectedPath)
    If file.ToLower.EndsWith(".doc") Then
        Dim t0 As Threading.Thread
        t0 = New Threading.Thread(AddressOf thread_sub1)
        t0.Start(file)
    End If
Next


--  作者:zhy400137
--  发布时间:2018/4/4 22:09:00
--  
mark
--  作者:天一生水
--  发布时间:2018/4/6 17:48:00
--  
按照原先的代码,和老师2楼提供的代码做了个对比的实例。
“多线程”按钮代码还有点问题,里面计时的“秒表”也不运行,需要老师帮助看看。
谢谢!
 下载信息  [文件大小:   下载次数: ]
图片点击可在新窗口打开查看点击浏览该文件:检索测试.rar



--  作者:有点甜
--  发布时间:2018/4/6 19:29:00
--  

1、多线程使用的时候,代码执行完毕,并不代表线程的代码执行完毕。你的提示信息是错误的。

 

2、提示信息,你可以写在线程里面,控制显示。

 

3、目前的代码,查询条件,是区分大小写的,这个要注意。(2楼问题绝对是没问题,请在读懂的基础上自行改造)

 

4、测试项目文件的时候,请不要用太多文件测试,一两个足够。