Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Blocking

Blocking

Scheduled Pinned Locked Moved C / C++ / MFC
comhelpquestion
14 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • _ _AnsHUMAN_

    Can you put the reading process in a thread?

    Somethings seem HARD to do, until we know how to do them. ;-)_AnShUmAn_

    T Offline
    T Offline
    tanarnelinistit
    wrote on last edited by
    #3

    I don't get it. What do you mean? Be more specific, please.

    _ 1 Reply Last reply
    0
    • T tanarnelinistit

      I don't get it. What do you mean? Be more specific, please.

      _ Offline
      _ Offline
      _AnsHUMAN_
      wrote on last edited by
      #4

      tanarnelinistit wrote:

      neverending loop to read informations from the COM

      Why don't you put this loop inside a thread function to read the COM port>?if you do so you can keep on reading the COM port in the thread and leave your main application's thread working.

      Somethings seem HARD to do, until we know how to do them. ;-)_AnShUmAn_

      1 Reply Last reply
      0
      • T tanarnelinistit

        Hello i'm using a neverending loop to read informations from the COM but the problem is that the reading process blocks my aplication. Not to listen to the COM all the time is not an option. What should I do to use the aplication in this time?

        E Offline
        E Offline
        Eytukan
        wrote on last edited by
        #5

        Create a worker thread and put your do { //your COM reading code }while; loop inside the Thread function.


        --[:jig:]-- [My Current Status] I dont know why the hell the script for voting 5 is disabled only for me?? :mad:

        T 1 Reply Last reply
        0
        • E Eytukan

          Create a worker thread and put your do { //your COM reading code }while; loop inside the Thread function.


          --[:jig:]-- [My Current Status] I dont know why the hell the script for voting 5 is disabled only for me?? :mad:

          T Offline
          T Offline
          tanarnelinistit
          wrote on last edited by
          #6

          and how do i create a worker thread?

          E T 2 Replies Last reply
          0
          • T tanarnelinistit

            and how do i create a worker thread?

            E Offline
            E Offline
            Eytukan
            wrote on last edited by
            #7

            simple... Follow these steps : you have a function. ReadCOM(). You dont want this to block your application. declare these in your header may be COMPROG.h //COMPROG.h static UINT MyThread(LPVOID lp); void ReadCOM(); in the implementation, COMPROG.cpp AfxBeginThread(MyThread,this);// Your call to start the thread. Your Thread: UINT MyThread(LPVOID lp) { CCOMPROG* obj = (CCOMPROG*)lp; //To access the ReadCOM function, or you can pass any function, with using the void pointer. obj->ReadCOM(); } Your Fcuntion: void ReadCom() { do { //Your Code to read COM }while(x==true);//your condition }

            T 1 Reply Last reply
            0
            • E Eytukan

              simple... Follow these steps : you have a function. ReadCOM(). You dont want this to block your application. declare these in your header may be COMPROG.h //COMPROG.h static UINT MyThread(LPVOID lp); void ReadCOM(); in the implementation, COMPROG.cpp AfxBeginThread(MyThread,this);// Your call to start the thread. Your Thread: UINT MyThread(LPVOID lp) { CCOMPROG* obj = (CCOMPROG*)lp; //To access the ReadCOM function, or you can pass any function, with using the void pointer. obj->ReadCOM(); } Your Fcuntion: void ReadCom() { do { //Your Code to read COM }while(x==true);//your condition }

              T Offline
              T Offline
              tanarnelinistit
              wrote on last edited by
              #8

              Thanks a lot I folowed your code but ended up with the folowing error message : error C4716: 'CMina_sView::MyThread' : must return a value; i tryed to insert a return 0 or return TRUE but that wasn't much of a help as I got the same result: program blocked. so??!?

              E 1 Reply Last reply
              0
              • T tanarnelinistit

                and how do i create a worker thread?

                T Offline
                T Offline
                ThatsAlok
                wrote on last edited by
                #9

                tanarnelinistit wrote:

                and how do i create a worker thread?

                http://www.flounder.com/workerthreads.htm[^]

                "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow

                cheers, Alok Gupta VC Forum Q&A :- I/ IV Support CRY- Child Relief and You

                1 Reply Last reply
                0
                • T tanarnelinistit

                  Thanks a lot I folowed your code but ended up with the folowing error message : error C4716: 'CMina_sView::MyThread' : must return a value; i tryed to insert a return 0 or return TRUE but that wasn't much of a help as I got the same result: program blocked. so??!?

                  E Offline
                  E Offline
                  Eytukan
                  wrote on last edited by
                  #10

                  it has a return type of UINT so do this, I missed it :( return(0); Now it should work. Otherwise you tell me. we'll kill it. also that , when you put your "readCOM()" function inside the class, implement it with your class(CCOMPROG) refernce like void CCOMPROG::readCOM()


                  --[:jig:]-- [My Current Status] I dont know why the hell the script for voting 5 is disabled only for me?? :mad:

                  T 1 Reply Last reply
                  0
                  • E Eytukan

                    it has a return type of UINT so do this, I missed it :( return(0); Now it should work. Otherwise you tell me. we'll kill it. also that , when you put your "readCOM()" function inside the class, implement it with your class(CCOMPROG) refernce like void CCOMPROG::readCOM()


                    --[:jig:]-- [My Current Status] I dont know why the hell the script for voting 5 is disabled only for me?? :mad:

                    T Offline
                    T Offline
                    tanarnelinistit
                    wrote on last edited by
                    #11

                    I'm afraid someone will have to get killed. :)) Let me tell you how i modified your code: 1) I use an OnListenComm() function that is as the name tells us: a function auto-defined by VC++ that handles the code when you click on listencom button. 2)The modifications: static UINT MyThread(LPVOID lp); afx_msg void OnListenCom1(); AfxBeginThread(MyThread,this);// Where should i put this??????? UINT CMina_sView::MyThread(LPVOID lp) { CMina_sView* obj = (CMina_sView*)lp; //To access the ReadCOM function, or you can pass any function, with using the void pointer. obj->OnListenCom1(); return 0; }

                    E 1 Reply Last reply
                    0
                    • T tanarnelinistit

                      Hello i'm using a neverending loop to read informations from the COM but the problem is that the reading process blocks my aplication. Not to listen to the COM all the time is not an option. What should I do to use the aplication in this time?

                      D Offline
                      D Offline
                      David Crow
                      wrote on last edited by
                      #12

                      Read here.


                      "Money talks. When my money starts to talk, I get a bill to shut it up." - Frank

                      "Judge not by the eye but by the heart." - Native American Proverb

                      1 Reply Last reply
                      0
                      • T tanarnelinistit

                        I'm afraid someone will have to get killed. :)) Let me tell you how i modified your code: 1) I use an OnListenComm() function that is as the name tells us: a function auto-defined by VC++ that handles the code when you click on listencom button. 2)The modifications: static UINT MyThread(LPVOID lp); afx_msg void OnListenCom1(); AfxBeginThread(MyThread,this);// Where should i put this??????? UINT CMina_sView::MyThread(LPVOID lp) { CMina_sView* obj = (CMina_sView*)lp; //To access the ReadCOM function, or you can pass any function, with using the void pointer. obj->OnListenCom1(); return 0; }

                        E Offline
                        E Offline
                        Eytukan
                        wrote on last edited by
                        #13

                        It depends on when you want to start the thread. For example, you have CButton , If you wanna start listening to COM on the click of it, put the AfxBeginThread inside the OnClick Event of the CButton. You can do that anywhere.


                        --[:jig:]-- [My Current Status] I dont know why the hell the script for voting 5 is disabled only for me?? :mad:

                        1 Reply Last reply
                        0
                        • T tanarnelinistit

                          Hello i'm using a neverending loop to read informations from the COM but the problem is that the reading process blocks my aplication. Not to listen to the COM all the time is not an option. What should I do to use the aplication in this time?

                          E Offline
                          E Offline
                          earl
                          wrote on last edited by
                          #14

                          You can also poll the COM port; this isn't necessarily the nicest design, but it may work. See the function SetCommTimeouts to set read timeouts. In fact, you may want to do this in either case; your thread can block reading the port and wake up every 1s to check if it is time to quit the application. Save yourself a bunch of hassle and do a bit of reading about thread safety issues now before you do a bunch of work. earl

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups