DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Threads problem

  1. #1
    cheickna Guest

    Threads problem


    Hi,

    I use Visual C++ 6.0 and i need help for the following problem.

    In my App, i have two command button. The first button send some instructions

    like this :

    bool NoData=false;
    while(1)
    {
    .....
    OpenPort(PortId); // Open a port com ( for exemple com2)
    WriteFile(.......); // Write to port com
    .....
    if(NoData)
    {
    ClosePort(PortId); //Close port com
    break;
    }
    }

    While my Button 1 send theses commands, i want to use button 2 to stop
    this operation before Variable NoData is true.
    To do this i try to close directly port com :

    ClosePort(PortId); //Close port com

    But it don't work, because i can press button 2 during this operation.


    Is anybody can help me How can i use two threads to do this

    Thanks in advance


  2. #2
    Scott Gines Guest

    Re: Threads problem


    To do this, you use the command AfxBeginThread. It would go something like
    this...

    UINT ProcessThread(LPVOID param)
    {
    CMyDialog* pDlg = (CMyDialog*)param;

    .....
    OpenPort(PortId); // Open a port com ( for exemple com2)
    WriteFile(.......); // Write to port com
    .....
    if(NoData)
    {
    ClosePort(PortId); //Close port com
    break;
    }

    if(pDlg->m_bCloseThread == true) // Check to see if we want to get out
    of the thread
    return 0;
    }

    ...

    void CMyDialog::OnButton1()
    {
    m_bCloseThread = false;
    AfxBeginThread(ProcessThread, this, THREAD_PRIORITY_NORMAL);
    }

    void CMyDialog::OnButton2()
    {
    m_bCloseThread = true;
    }

    Make sure that you have a m_bCloseThread as a public member of CMyDialog.


    "cheickna" <cheickna.traore@almas.fr> wrote:
    >
    >Hi,
    >
    >I use Visual C++ 6.0 and i need help for the following problem.
    >
    >In my App, i have two command button. The first button send some instructions
    >
    >like this :
    >
    >bool NoData=false;
    >while(1)
    >{
    > .....
    > OpenPort(PortId); // Open a port com ( for exemple com2)
    > WriteFile(.......); // Write to port com
    > .....
    > if(NoData)
    > {
    > ClosePort(PortId); //Close port com
    > break;
    > }
    >}
    >
    >While my Button 1 send theses commands, i want to use button 2 to stop
    >this operation before Variable NoData is true.
    >To do this i try to close directly port com :
    >
    > ClosePort(PortId); //Close port com
    >
    >But it don't work, because i can press button 2 during this operation.
    >
    >
    >Is anybody can help me How can i use two threads to do this
    >
    >Thanks in advance
    >



  3. #3
    cheickna Guest

    Re: Threads problem


    For this you also need thread synchronisation using critical section or mutex
    or event.
    for further clarification mail to gopal@cresofsol.com

    "Scott Gines" <nospamsgines@qcomm.com> wrote:
    >
    >To do this, you use the command AfxBeginThread. It would go something like
    >this...
    >
    >UINT ProcessThread(LPVOID param)
    >{
    > CMyDialog* pDlg = (CMyDialog*)param;
    >
    > .....
    > OpenPort(PortId); // Open a port com ( for exemple com2)
    > WriteFile(.......); // Write to port com
    > .....
    > if(NoData)
    > {
    > ClosePort(PortId); //Close port com
    > break;
    > }
    >
    > if(pDlg->m_bCloseThread == true) // Check to see if we want to get

    out
    >of the thread
    > return 0;
    >}
    >
    >...
    >
    >void CMyDialog::OnButton1()
    >{
    > m_bCloseThread = false;
    > AfxBeginThread(ProcessThread, this, THREAD_PRIORITY_NORMAL);
    >}
    >
    >void CMyDialog::OnButton2()
    >{
    > m_bCloseThread = true;
    >}
    >
    >Make sure that you have a m_bCloseThread as a public member of CMyDialog.
    >
    >
    >"cheickna" <cheickna.traore@almas.fr> wrote:
    >>
    >>Hi,
    >>
    >>I use Visual C++ 6.0 and i need help for the following problem.
    >>
    >>In my App, i have two command button. The first button send some instructions
    >>
    >>like this :
    >>
    >>bool NoData=false;
    >>while(1)
    >>{
    >> .....
    >> OpenPort(PortId); // Open a port com ( for exemple com2)
    >> WriteFile(.......); // Write to port com
    >> .....
    >> if(NoData)
    >> {
    >> ClosePort(PortId); //Close port com
    >> break;
    >> }
    >>}
    >>
    >>While my Button 1 send theses commands, i want to use button 2 to stop


    >>this operation before Variable NoData is true.
    >>To do this i try to close directly port com :
    >>
    >> ClosePort(PortId); //Close port com
    >>
    >>But it don't work, because i can press button 2 during this operation.
    >>
    >>
    >>Is anybody can help me How can i use two threads to do this
    >>
    >>Thanks in advance
    >>

    >



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center