VBScript线程 [英] VBScript threading

查看:70
本文介绍了VBScript线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

前一段时间,我在vbscript中粉碎了这个多线程代码,然后又将它再次拉出来,以将其应用于另一个问题.我现在遇到一个问题,其中运行命令(第20行)的返回值始终为0.我意识到我已经将False用作WaitOnReturn参数,但是不会以其他方式线程化.我很确定它能完美运行,否则我不会保存那么久...

A while back I smashed together this multithreading code in vbscript and I've just pulled it out again to apply it to another problem. I'm now having an issue where the return value of the run command (line 20) is always 0. I realize I've used False as the WaitOnReturn argument, however it won't thread otherwise. I'm pretty sure it worked perfectly or I wouldn't have kept it for so long...

有人可以看到我做错了吗?

Can anyone see what I've done wrong?

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

推荐答案

抱歉,wsh.Run()没有任何想象力.它开始一个新进程,而不是线程.

Sorry, wsh.Run() is not threading by any stretch of the imagination. It starts a new process, not a thread.

如果不使用第4个参数,则返回0是预期的结果.文档在这里.

Getting 0 as a return is the expected outcome if you do not use the 4th argument. Documentation is here.

这篇关于VBScript线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆