How to start two threads at the same time

Hi there,
  I want to include multithreading in the CVI program. I have two tasks which has no dependence between them, so I would like to start two threads for perform the tasks. I follow the introduction http://www.ni.com/white-paper/3663/en/ to start my code. In my code, I add a command button, when it is pressed, I create two threads to perform the tasks. In each task, I have a big loop and in each iteration, I use ProcessSystemEvents to avoid no response on system message. But it doesn't work actually, the program suspended. I wonder must I initialize the threads in main. I am looking for a way to start the threads in the callback function for a button since I am not starting those threads at the beginning but while it is triggered.
the main frame of the code looks
int CVICALLBACK Test1(void *functionData)
{ int i=0;
for (i = 0; i<100000; i++) {ProcessSystemEvents();}
return 0;
int CVICALLBACK Test2(void *functionData)
int i = 0;
for (i = 0; i<100000; i++) {ProcessSystemEvents();}
return 0;
int CVICALLBACK cmdcb(int panel, int control, int event, void *callbackData, int eventData1, int eventData2)
{ int threadID1, threadID2;
if (event==EVENT_COMMIT)
SetPanelAttribute(panelHandle, ATTR_DIMMED, 1);
CmtScheduleThreadPoolFunction(DEFAULT_THREAD_POOL_HANDLE, Test1, NULL, &threadID1);
CmtScheduleThreadPoolFunction(DEFAULT_THREAD_POOL_HANDLE, Test2, NULL, &threadID2);
CmtWaitForThreadPoolFunctionCompletion (DEFAULT_THREAD_POOL_HANDLE, threadID1, 0);
CmtWaitForThreadPoolFunctionCompletion (DEFAULT_THREAD_POOL_HANDLE, threadID2, 0);
return 0;
Attachments:
TestThread.zip ‏239 KB

In my opinion your threads may be simply lasting longer than expected due to an incorrect sleep policy of threads paired with with ProcessSystemEvents () in the loop.
I made up a quick example that shows this behaviour: playing with "Do not sleep" and "Call ProcessSystemEvents" options you can see how threads execute.
BTW: do not forget to release the thread function IDs at the end of functions.
Proud to use LW/CVI from 3.1 on.
My contributions to the Developer Zone Community
If I have helped you, why not giving me a kudos?
Attachments:
thread.zip ‏7 KB

Similar Messages

  • Can't start two listeners at the same time

    Hello -
    I am running Oracle 10g R2 on Solaris 10. I am getting the following error when I try to start my second listener "TNS-01106: Listener using listener name ccn_listener has already been started". There are multiple VIPA's assigned to the server, so I am trying to start two listeners at the same time (I can start each one separately). The different IP's are referenced in the listeners (even though they use port 1521), so there should not be a conflict. I also gave the two listeners distinct non-default names. The listeners are running under two differenct Oracle homes.
    Here is the code of the listeners:
    listener 1:
    PHD_LISTENER =
    (DESCRIPTION_LIST =
    (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = 10.49.236.119)(PORT = 1521))
    (ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC0))
    SID_LIST_PHD_LISTENER =
    (SID_LIST =
    (SID_DESC =
    (SID_NAME = PLSExtProc)
    (ORACLE_HOME = /u02/app/oracle/product/10.2.0/db_1)
    (PROGRAM = extproc)
    (SID_DESC =
    (GLOBAL_DBNAME=phdv410g)
    (ORACLE_HOME = /u02/app/oracle/product/10.2.0/db_1)
    (SID_NAME = phdv410g)
    listener 2:
    CCN_LISTENER =
    (DESCRIPTION_LIST =
    (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = 10.49.236.131)(PORT = 1521))
    (ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC0))
    SID_LIST_CCN_LISTENER =
    (SID_LIST =
    (SID_DESC =
    (SID_NAME = PLSExtProc)
    (ORACLE_HOME = /u01/app/oracle/product/10.2.0/db_1)
    (PROGRAM = extproc)
    (SID_DESC =
    (GLOBAL_DBNAME=ccn10gt1)
    (ORACLE_HOME = /u01/app/oracle/product/10.2.0/db_1)
    (SID_NAME = ccn10gt1)
    If anyone has any ideas about why I can't start these two listeners togethers, I would really appreciate it.
    Thanks!

    Hi,
    Look at the error: "Listener already started"...
    Can you check if that is right ?
    ps -ef | egrep lsnrWaiting for feedback.
    Regards,
    Yoann.

  • How can I start two Vi at the same time?

     I have two Vi's, each of these Vi has a stop button. I want to start these two Vis at the same time. I also want to be able to stop the two Vis at the same time. What should I do? Thanks.

    One way to start them is with an Invoke Node and the Run method. You can then stop them with the Set Control Value [Variant] method. When you use the Set Control Value [Variant], you just have to specify the name of the front panel control that stops the while loop. Older versions of LabVIEW only had the Set Control Value method in which you had to use the flatten to string function to pass the Type Descriptor and Flattened Data.
    Message Edited by Dennis Knutson on 03-20-200609:35 AM
    Attachments:
    Run Method.JPG ‏44 KB

  • How to play two videos at the same time

    Hi everyone!
    Ok, so what I'm trying to do is basically play two videos, which are already saved on the computer, at the same time. I can get one video to play, but when i try to start another it just displays a blank screen.
    import javax.swing.*;
    import javax.media.*;
    import java.awt.*;
    import javax.media.protocol.*;
    public class TwoVideoTest {
         public static void main(String[]args) {
         MediaLocator locator1 = null;
         MediaLocator locator2 = null;
         try {
         locator1 = new MediaLocator("file:/C:/truck1.mpg");
         locator2 = new MediaLocator("file:/C:/truck2.mpg");
         } catch(Exception e) {
         System.out.println("COULNDT CREATE LOCATORS IN MAIN TEST CLASS");
                          System.exit(-1);
         Player player1 = null;
         Player player2 = null;
                     try {
          player1 = Manager.createRealizedPlayer(locator1);
         player2 = Manager.createRealizedPlayer(locator2);
                      } catch(Exception e) {
              System.exit(0);
         JFrame frame = new JFrame();
         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel panel1 = new JPanel();
       JPanel panel2 = new JPanel();
        panel1.add(player1.getVisualComponent());
       frame.getContentPane().add(panel1);
       frame.setSize(new Dimension(500,500));     
       frame.setVisible(true);
       player1.start();
       player2.start();
    }

    HELLO AGAIN! I guess i post too soon....
    So i modified my code and now the videos play together. Threads, sigh.
    But the audio that comes with the video doesn't appear to be starting at the same time. And this problem I ASSURE you i have no clue how to handle. And yes i know, the two video audios will overlap, but this a test for heavens sake!
    Anyways, heres the modified code in case it will help anyone
    import javax.swing.*;
    import javax.media.*;
    import java.awt.*;
    import javax.media.protocol.*;
    public class Video implements Runnable{
         String url;
         MediaLocator locator;
         Player player;
         JPanel panel;
         public Video (String url,JPanel panel) {
              this.url = url;
              try {
                   locator = new MediaLocator(url);
                   player = Manager.createRealizedPlayer(locator);     
                   panel.add(player.getVisualComponent());
                   player.start();
              } catch (Exception e) {
                   System.out.println("ERROR CREATING MEDIALOCATOR");
                   System.exit(-1);
         public void run() {
              try {
              Thread.sleep(100);
              } catch(Exception e) {}
         public static void main(String[]args) {
            JFrame frame = new JFrame();
              frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              JPanel panel1 = new JPanel();
              JPanel panel2 = new JPanel();
              panel1.setPreferredSize(new Dimension(100,100));
              panel2.setPreferredSize(new Dimension(100,100));
              Thread thread1 = new Thread(new Video("file:/C:/truck1.mpg",panel1));
              Thread thread2 = new Thread(new Video("file:/C:/truck2.mpg",panel2));
              frame.getContentPane().add(panel1);
              frame.getContentPane().add(panel2);
              frame.setSize(new Dimension(500,500));     
              frame.setVisible(true);
              thread1.start();
              thread2.start();
         

  • How to process two files at the same time using BPM

    Hi experts,
    I have a Integration Process it takes two files one Header xml and one Lines xml. These files shoudl be processed at the same time because them will be mappend in a Idoc.
    The realy trouble is that these inbound xmls never are executed at the same time. Then a IP is executed for each inbound xml.
    This is the IP:
    http://picasaweb.google.com/hose86/SapXi#5377539387476591346
    This is related documentation:
    http://help.sap.com/saphelp_nwpi71/helpdata/en/cb/15163ff8519a06e10000000a114084/frameset.htm
    I try to do all what I know, but It don't work fine :S
    Thanks in advance and Regards,
    Jose Antonio.

    Hi Jose,
    Don't specify any end condition for  fork step and check the start process check box in both the receive step.
    Also put all steps inside the block.. so that the control step in deadline branch can throw exception and after that the BPM ends. As per the design now, if a deadline exception is raised (it will b handled) but after that the transformation step and send step is executed. There the BPM fails.
    Regards
    Suraj
    Edited by: S.R.Suraj on Sep 4, 2009 5:39 AM

  • How to select two things at the same time ?

    Hey guys, i'm French and I need your help !
    I'm working everyday on iWork and especially Pages.
    How to select two parts of a text at the same time ?
    Before we only need to select one, to press the "cmd" button and then to select another part. It doesn't work anymore!
    Thanks for you help
    and sorry for the mistakes !

    Hey guys, i'm French and I need your help !
    I'm working everyday on iWork and especially Pages.
    How to select two parts of a text at the same time ?
    Before we only need to select one, to press the "cmd" button and then to select another part. It doesn't work anymore!
    Thanks for you help
    and sorry for the mistakes !

  • How to avoid two threads creating the same entity?

    I seem to have a problem with a race-condition. Two threads are trying to
              create the same entity. One thread calls a finder with no success and
              creates the entity, meanwhile - before the entity gets persisted
              (ejbStore()) - the other thread also calls a finder with no success,
              creating the
              same entity.
              My transaction isolation level in the weblogic-ejb-jar.xml is
              <transaction-isolation>
              <isolation-level>TRANSACTION_READ_UNCOMMITTED</isolation-level>
              <method>
              <ejb-name>ejb</ejb-name>
              <method-name>*</method-name>
              </method>
              </transaction-isolation>
              I've also tried <isolation-level>TRANSACTION_SERIALIZABLE</isolation-level>
              with no significant change in behavior.
              In the ejb-jar.xml
              <container-transaction>
              <method>
              <ejb-name>ejb</ejb-name>
              <method-name>*</method-name>
              </method>
              <trans-attribute>Required</trans-attribute>
              </container-transaction>
              

              Try setting delay-updates-until-end-of-tx to false for your bean. Try experimenting
              with concurrency-strategy of Exclusive. Or re-write your code to not check for
              existence before creation; just create the bean and let the db return a duplicate
              key exception.
              simon.
              "Daniel" <[email protected]> wrote:
              >I seem to have a problem with a race-condition. Two threads are trying
              >to
              >create the same entity. One thread calls a finder with no success and
              >creates the entity, meanwhile - before the entity gets persisted
              >(ejbStore()) - the other thread also calls a finder with no success,
              >creating the
              >same entity.
              >
              >My transaction isolation level in the weblogic-ejb-jar.xml is
              ><transaction-isolation>
              ><isolation-level>TRANSACTION_READ_UNCOMMITTED</isolation-level>
              ><method>
              ><ejb-name>ejb</ejb-name>
              ><method-name>*</method-name>
              ></method>
              ></transaction-isolation>
              >
              >I've also tried <isolation-level>TRANSACTION_SERIALIZABLE</isolation-level>
              >with no significant change in behavior.
              >
              >In the ejb-jar.xml
              ><container-transaction>
              ><method>
              ><ejb-name>ejb</ejb-name>
              ><method-name>*</method-name>
              ></method>
              ><trans-attribute>Required</trans-attribute>
              ></container-transaction>
              >
              >
              

  • How to print two PLD at the same time

    Dear Expert,
    I have made two PLD on the purchase order (Page 1 & Page 2 both are the saperate PLD's)
    Is there any shortcut or function available in the SBO that when I print the PLD 1 system has to print the 2 nd PLD without any cammand.
    Regards,
    Datta Kharat

    Hi Datta Kharat,
    I think, It is not possible in SAP B1.
    so, Try to Create Single PLD and Use Number of Layout Pages -> 2 in PLD.
    ->> Open the Require PLD.
    ->> Right Click in PLD and Choose the Display Document Properties.
    ->> Assign the Number of Layout Pages = 2 in Paper Format Tab on Document Properties Window.
    Regards,
    Madhan.

  • How to open two projects in the same time?

    I have over 40 file project. Stadium.
    I am in finish of my work, but I forgot about something. I didn't notice that I lost a few keyfreme animation. I didn't realize that in 30 project and I am more over 40 right now. I made a lot of work and I can't go back to 30 project. I don't want to do this from the beginning, so I would like to COPY my keyfremes from project 3o to project 40. To do that I need to have 2 programs opens (to copy keyfremes from 30 project to 40 project). But I can't open two After Effects.
    How Can I copy my keyframes on timeline?
    To copy ALL of this in one turn and paste also:

    By FILE - IMPORT - FILE? It's just simple?
    Thank you!

  • How to update two tables at the same time using jdbc

    Plz will anybody tell me what is the code or query in jdbc by which you insert entry in one table and simultaneously it goes in another table?
    Thanks in advance

    You might be
    able to design a trigger to do what you're talking
    about, but that would be a database thing, not a java
    thingAnother option would be an updatable view (google for usage), but it has limitations and should be used with caution. And yes, its a database thinggy and not java.
    cheers,
    ram.

  • Updating two tables at the same time

    Please,
    Does someone have an idea on how to update two tables at the same times?
    Thanks a lot in advance

    I was thinking about a single session and single update command.
    That's why I thougth someone else could have a clue.
    Thank you

  • How to start a thread after the execution/termination of another thread.???

    Hello,
    How to start a thread after the execution/termination of another thread....???
    For example:
    consider two threads: ThreadA and ThreadB
    I could start the ThreadB after the execution of ThreadA.
    How to identify the termination of one thread.....???
    Please help me...
    Thanks & Regards,
    Kayalvizhi

    What do you mean with "it doesn't work"? Do you get a compiler error? Do you get an exception at runtime? What's the error message?
    Jesper

  • How to run two query  in the same preparedStatement

    Hi all,
    Please tell me how to run two queries in the same preparedStatement object ?
    In my module I have to run two queries and I don't want to make two methods for running two different queries.
    I just want to call this method for both of my queries.
    methodName(long param)
    Connection conn=null;
    PreparedStatement pstmt=null;
    //////////////// some coding
    Please Help !
    Thanks in advance
    amitindia

        public void foo()
            Connection connection = null;
            PreparedStatement stmt = null;
            try {
                connection = ...get from pool...;
                stmt = connection.prepareStatement("...");
                ...fetch results...;
                stmt.close();
                stmt = null; // So the finally statement works if there is an exception
                stmt = connection.prepareStatement("...");
                ...fetch results...;
            } finally {
                if (stmt != null) ..close it...;
                if (connection != null) ..return it to pool...;
        }

  • I am very disappointed with iTunes.  I have 1200 songs and I can't play them.  I try to play them and before one song finish a song starts playing. This then results in hearing two songs at the same time. I will like to know if you can fix this. I feel li

    I am very disappointed with iTunes.  I have 1200 songs and I can’t play them.  I try to play them and before one song finisha song starts playing. This then results in hearing two songs at the same time.I will like to know if you can fix this. I feel like listening with the iTunesplayer is useless.

    I don't know man I too hope apple will do something this new I-tunes blow my top lost my whole tune!!!

  • How to get two parameters in the same line of selection screen?

    hello
    i need to get my selection csreen like bellow.
    r1 radiobuttion  -some space --p1 parameter
    i should not get the parameter in the next line of  radiobuttion.
    how to get two parameters in the same line of selection screen?

    hi....
    modify the following code
    it will work
    SELECTION-SCREEN BEGIN OF BLOCK SL1 WITH FRAME TITLE TEXT-003.
    SELECTION-SCREEN BEGIN OF LINE.
    SELECTION-SCREEN COMMENT 10(15) TEXT-001
                     FOR FIELD P1.
    SELECTION-SCREEN POSITION POS_LOW.
    PARAMETERS : P1 TYPE   C USER-COMMAND R2 RADIOBUTTON GROUP R2 DEFAULT 'X',
      P2 TYPE SCARR-CARRNAME,
      P3 TYPE CHAR1 RADIOBUTTON GROUP R2.
    SELECTION-SCREEN END OF LINE.
    SELECTION-SCREEN END OF BLOCK SL1.

Maybe you are looking for

  • Payload error DW031

    I am trying to load CS 5.5 on to a new computer with Windows 8.1 and Ilustrator will not load. Warning:DW031 and Fatal error: DW020. what is going on?

  • I want to allocate balance qty in sql.....

    Please help me… I want to allocate balance qty in sql..... I have data like below table and expected answer in column EDIT_UP_TO_EXEPECTED_ANS. I want to edit data with validation of next process executed qty. for example in row 3rd and 4th PROCESS_E

  • Start Routine Logic Updating Field in Target ODS

    Hi, I am creating a  Start Routine to Update Field (date) from one ODS to another based on the Key Structure: Document Number, Line Item Number. I am encountering a situation as follows: Source Doc Num 1 Line Item 10 Date 1/1/1 Target Doc Num 1 Line

  • Transfer Video to client

    can you please help me to know how server when play video ,can send stream to client so that client can watch movie on his media player without downloading file from server .

  • Send language information to ocx client

    We are running a java application server. One of the client is a c++ client (ocx). We need to send send language information to the client so it startes up with the right language defined from the server. Should the language information be stored in