Showing results 1 to 7 of 7

Thread: Multithreading with PCE and VBScript

  1. #1
    mrk Gast

    Default Multithreading with PCE and VBScript

    Hi Folks!

    I have a tremendous problem!
    The Problem:

    I have programmed a simulation. In the simulation there is a long loop. When this loop is working i can't do anithing. So i thought i should program it with multithreading. And now to my problem. I have created a new Task and copied in the following code:

    Dim obVar
    
    Do While thisProject.Variables.Item(RoboterPos_X).Value = 500
    
              DoEvents
    
                   
     thisProject.Variables.Item(RoboterPos_X).Value = thisProject.Variables.Item(RoboterPos_X).Value + 1
    
     
    Loop
    
    MsgBox thisProject.Variables.Item(RoboterPos_X).Value

    When i start the task, the msgbox gives me the number, which was the number, when i started the program.
    So the problem is, that the loop doesn't work.

    Has anyone an idea how this code could work?

    Please help me.

    Yours, Mr.K

  2. #2

    Default Re: Multithreading with PCE and VBScript

    it is generally not advisable to do:

    - increment loops
    - write / read commands on variables which might not be advised
    - popup messageboxes
    - use DoEvents

    either in VBA or in the PCE.

    Also it is not recommended accessing the variables in the PCE by "thisproject.variables.item" as it will access the runtime and might block the runtime-thread.

    In PCE:

    'The Mainfunction is a cyclic function
    'myVar is the variable linked at the task in PCE
    'please mind, that vbscript is case-sensitive!
    
    Sub Task_Main()
    If (Not Task.Value("myVar") = 500) Then
    	Task.Value("myVar") = Task.Value("myVar") +1
    End If
    End Sub

    There are several other, better ways of implementing counter functions. One way is to use the simulation driver. Another way is to use time-control and set-value functions. Limits with delays could also be used. Another, more convenient, more flexible and much more powerful way, is to use STRATON as an external logic. (with real thread independancy)

  3. #3
    mrk Gast

    Default Re: Multithreading with PCE and VBScript

    thx for the answer
    but when i use this code:

    If (Not Task.Value("RoboterPos_X") = 500) Then
        Task.Value("RoboterPos_X") = Task.Value("RoboterPos_X") +1
    End If
    
    MsgBox Task.Value("RoboterPos_X")

    the Variable RoboterPos_X doesn't change the number. What is the reason for that??
    Please Help me!

    Yours, Mr.K

  4. #4

    Default Re: Multithreading with PCE and VBScript

    please find attached a small sample project in zenOn 6.22 SP0 which moves the numerical value in the picture with PCE, using the vbscript code above.

    Again, please note that there are better ways to create such a simulation!

    Did you link the correct variable at the task? Maybe the variable is "read-only"?

    you could temporarily put a messagebox in your PCE code within the If statement to see whether the code to increment is executed or not.

    2008-03-18_17-10-51 pce.zip

  5. #5
    mrk Gast

    Default Re: Multithreading with PCE and VBScript

    oh thank you! it works.

    But i have another question.

    How can i stop the task, which is running, with a query in this tast which is running.
    More simple: how can i stop a task with the task.

    Please help me!
    Thx

    Yours, MrK

  6. #6

    Default Re: Multithreading with PCE and VBScript

    you could add another boolean variable to the task and another if statement over the the counter if statement. If the boolean variable is true (1) then do the counting. If the boolean variable is false (0) then just nothing is done.

    This is not actually stopping the task, but you can control if the procedure in the task is excuted, by a variable in zenOn.

    'The Mainfunction is a cyclic function
    Sub Task_Main()
    If (Task.Value("myVar2") = 1) Then
    	If (Not Task.Value("myVar") = 500) Then
    		Task.Value("myVar") = Task.Value("myVar") +1
    	End If
    End if
    End Sub
    
    'Exitfunction

    The PCE tasks itself can be controlled by VBA.

  7. #7
    mrk Gast

    Default Re: Multithreading with PCE and VBScript

    Thank you for the great information! It helped me a lot!!!
    Thank you very much!!!

    See you, Mr.K

Posting Rules

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •