×

Loading...
Ad by
Ad by

向VB高手求救: 请见内文?

我有两步操作, 分别是模拟浏览器的操作访问某网页. cmdAction1_Click and cmdAction2_Click . 分别执行, 没有问题. 其中 第二步需要根据第一步的结果来执行.

现在, 我想这两步自动执行. 代码如下:

Private Sub cmdDoAll_Click()
Call cmdAction1_Click
MsgBox ("go!")
Call cmdAction2_Click
End Sub

系统弹出Msgbox , 我按Ok , 后, 两步如期执行. 但是我还得按一下键盘, 我想省去这步. 然后用如下代码:

Private Sub cmdDoAll_Click()
Call cmdAction1_Click
sleep 15000
Call cmdAction2_Click
End Sub

可是这样一来, 第二不根本好象不执行.

请解释为什么?! 有何良策? 谢谢!
Report

Replies, comments and Discussions:

  • 工作学习 / IT技术讨论 / 向VB高手求救: 请见内文?
    我有两步操作, 分别是模拟浏览器的操作访问某网页. cmdAction1_Click and cmdAction2_Click . 分别执行, 没有问题. 其中 第二步需要根据第一步的结果来执行.

    现在, 我想这两步自动执行. 代码如下:

    Private Sub cmdDoAll_Click()
    Call cmdAction1_Click
    MsgBox ("go!")
    Call cmdAction2_Click
    End Sub

    系统弹出Msgbox , 我按Ok , 后, 两步如期执行. 但是我还得按一下键盘, 我想省去这步. 然后用如下代码:

    Private Sub cmdDoAll_Click()
    Call cmdAction1_Click
    sleep 15000
    Call cmdAction2_Click
    End Sub

    可是这样一来, 第二不根本好象不执行.

    请解释为什么?! 有何良策? 谢谢!
    • 我也有过这种情况,后来有人知道,既然在1后面肯定要执行2,那为什么不把2放到1中去调用呢,不要放在主的地方调用,这样既能保证2肯定得到1的参数,又能提高程序效率
      • 那样做不还是一样吗? 那为什么, 用Msgbox 以后就没问题了?! 而用Sleep 就不行了? 请教.
        • 有可能是程序的并行处理,不知道1能不能在sleep15000前做完
      • 那你知道如何通过代码来模拟 "回车" 吗?
        • SendKeys "{ENTER}"
        • 我也是初学者,只懂一点
    • "sleep" stops the current execution, including the "command1". try "timer" if the delay is neccessary.
      • I got it . just coz sleep will stop all execuation , which is no good . using timer makes it better . Thanks buddy .
    • 示例代码见内
      private cmdAction1NotFinished as boolean
      Private Sub cmdDoAll_Click()
      cmdAction1NotFinished =false
      Call cmdAction1_Click
      while cmdAction1NotFinished
      DoEvents
      wend
      Call cmdAction2_Click
      End Sub

      private sub IE_DocumentFinished( url....)
      cmdAction1NotFinished =true
      end sub
      • 呵呵,这个代码我做过,这样,在1 做完前 cpu的占用率 一直是 100%,被我的 director 看见了,惨哪
      • you not kidding, are u?
        timer is a better solution, I would say, as someone above said,
        the only different is you have to figure out a better way to detect
        the finish-event of 1st command, i always worry about dead-loop
        kind of things
        • 其实我是想说:不能用sleep。因为主线程sleep会无法接收到第一个call完成时的消息。其实更合理的作法是在第一个call完成的消息里执行第二个call
    • use vb's timer, it works in thread mode.
      • 请教一个问题:VB中如何把一个异步方法转为同步方法?其实这和正在讨论的问题是一样的,不能sleep,不能timer,如果用循环又会占用CPU,高手有什么办法吗?
        • VB好象没办法实现VC的那种多线成, 以前试验而已, 即使去得线程序号也控制不了。 不过大概手低, 不灵
        • 你不是自己解决了嘛,互相调用
          • 不明白,能详细一点吗?我正要做一个function,把一个异步调用的方法包装成一个同步方法,是不容易
            • 偶以前做过,最近没空,天天在TTC上横贯toronto,劳命伤财,过两天再来看你们做好了没。
        • 1, I think you can you Semaphore, mutex this kind of API, for example, Semaphore, WaitForSingleObject works in the way u want)
          2, develop a dll using VC, and send a handle( for example , edit) to dll, and in the dll develop a function, using SendMessage, PostMessage, post the message to the handle's holder, in the holder's event(for example edit's onchange) deal with defferent message useful to you.
    • use event, not doEvent