首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >VBScript线程化

VBScript线程化
EN

Stack Overflow用户
提问于 2011-01-06 08:29:23
回答 1查看 14.1K关注 0票数 3

不久前,我用vbscript把这个多线程代码拼凑在一起,然后我又把它拿出来用来解决另一个问题。我现在遇到一个问题,run命令(第20行)的返回值始终为0。我意识到我使用了False作为WaitOnReturn参数,但是它不会线程。我很确定它工作得很好,否则我不会保存这么久……

有没有人看到我做错了什么?

代码语言:javascript
复制
Class Thread
' Usage:
'    Dim x: Set x = New Thread
'    Call x.init(10)
'    Call x.queue("script.bat", Array("Arg1", "output_file.txt"))
'    Call x.queue("cscript.exe prog.vbs", Array("Arg1", "Arg2", "Arg3"))
'    Call x.setMax(20)
''''''''''''''''''''''''''''''''''''''''''''''
   Private p_threads
   Private p_max

   Private Function spawn(action, args)
      Dim wsh: Set wsh = WScript.CreateObject("WScript.Shell")
      Dim command: command = action
      Dim element
      For Each element In args
         command = command & " " & element
      Next
      spawn = wsh.Run(command, 0, False)
      Set wsh = Nothing
   End Function

   Public Sub queue(action, args)
      If Ubound(p_threads,1) < p_max Then
         ' create new thread
         ReDim Preserve p_threads(Ubound(p_threads, 1)+1)
         p_threads(Ubound(p_threads, 1)) = spawn(action, args)
      Else
         ' recycle old thread
         Do
            Dim i
            For i = 1 To Ubound(p_threads, 1)
               ' find a thread which has finished
               If p_threads(i) = 1 Then
                  p_threads(i) = spawn(action, args)
                  Exit Sub
               End If
            Next
            ' wait for a thread to finish
            WScript.Sleep 300
         Loop Until False
     End If
   End Sub

   Public Sub init(n)
      p_threads = Array(1)
      p_max = n
   End Sub

   Public Property Let setMax(n)
      p_max = n
   End Property
End Class
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-01-06 09:20:20

很抱歉,wsh.Run()没有任何想象力的线程化。它启动一个新的进程,而不是一个线程。

如果不使用第四个参数,则返回0是预期的结果。文档is here

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4610686

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档