Thread

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Kate Luu

    #1

    Thread

    Hi All,

    First of all, My computer have only one comm port, and I did set up the port
    as looping call back. I have one form with one button. When the form load, I
    call WriteFile to open the port and then create a thread for reading back
    the port if some thing arrive into the port. When I click the button, I
    write the word: "Hello World" to the comm port. When data access the port,
    the second thread was trigger. when the second thread read a first
    character, its gave up its thread to its parent, it doesn't read the whole
    thing as "Hello World". How can I prevent the second thread gave up its
    thread to its parent until it's finish reading all the characters were came
    to the port. Any help is greatly appreciated.

    Kate Luu


  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: Thread

    Kate,

    I am not quite sure what you are trying to do, but in your callback, you
    might want to check until you have read all of the information that came
    through the port, caching it until you want to read it all. When you
    finally know you have enough data, then you can continue processing.

    Hope this helps.


    --
    - Nicholas Paldino [.NET/C# MVP]
    - nicholas.paldin o@exisconsultin g.com

    "Kate Luu" <cql90@yahoo.co m> wrote in message
    news:OHtiYTmSDH A.632@tk2msftng p13.phx.gbl...[color=blue]
    > Hi All,
    >
    > First of all, My computer have only one comm port, and I did set up the[/color]
    port[color=blue]
    > as looping call back. I have one form with one button. When the form load,[/color]
    I[color=blue]
    > call WriteFile to open the port and then create a thread for reading back
    > the port if some thing arrive into the port. When I click the button, I
    > write the word: "Hello World" to the comm port. When data access the port,
    > the second thread was trigger. when the second thread read a first
    > character, its gave up its thread to its parent, it doesn't read the whole
    > thing as "Hello World". How can I prevent the second thread gave up its
    > thread to its parent until it's finish reading all the characters were[/color]
    came[color=blue]
    > to the port. Any help is greatly appreciated.
    >
    > Kate Luu
    >
    >[/color]


    Comment

    • Kate Luu

      #3
      Re: Thread

      Thanks you so much for your help, I'm apprecated. I did have a loop to read
      all the character, it is look like this:

      do

      {

      gotbytes = 0;

      if( !Win32Com.ReadF ile( hPort, buf, 1, out gotbytes, unmanagedOv ) )

      {

      throw new CommPortExcepti on("IO Error [004]");

      }

      if( gotbytes == 1 )

      OnRxChar( buf[0] );

      } while( gotbytes > 0 );



      But because this project support OVERLAPPED, mean Asynchronous, therefore
      the thread just jump back and forth between window thread( parent thread )
      and user thread. It must be able to control the parent thread some how but
      I'm really don't know how to do it. Have a good one...

      Kate Luu





      "Nicholas Paldino [.NET/C# MVP]" <nicholas.paldi no@exisconsulti ng.com> wrote
      in message news:#se0oOxSDH A.1724@TK2MSFTN GP10.phx.gbl...[color=blue]
      > Kate,
      >
      > I am not quite sure what you are trying to do, but in your callback,[/color]
      you[color=blue]
      > might want to check until you have read all of the information that came
      > through the port, caching it until you want to read it all. When you
      > finally know you have enough data, then you can continue processing.
      >
      > Hope this helps.
      >
      >
      > --
      > - Nicholas Paldino [.NET/C# MVP]
      > - nicholas.paldin o@exisconsultin g.com
      >
      > "Kate Luu" <cql90@yahoo.co m> wrote in message
      > news:OHtiYTmSDH A.632@tk2msftng p13.phx.gbl...[color=green]
      > > Hi All,
      > >
      > > First of all, My computer have only one comm port, and I did set up the[/color]
      > port[color=green]
      > > as looping call back. I have one form with one button. When the form[/color][/color]
      load,[color=blue]
      > I[color=green]
      > > call WriteFile to open the port and then create a thread for reading[/color][/color]
      back[color=blue][color=green]
      > > the port if some thing arrive into the port. When I click the button, I
      > > write the word: "Hello World" to the comm port. When data access the[/color][/color]
      port,[color=blue][color=green]
      > > the second thread was trigger. when the second thread read a first
      > > character, its gave up its thread to its parent, it doesn't read the[/color][/color]
      whole[color=blue][color=green]
      > > thing as "Hello World". How can I prevent the second thread gave up its
      > > thread to its parent until it's finish reading all the characters were[/color]
      > came[color=green]
      > > to the port. Any help is greatly appreciated.
      > >
      > > Kate Luu
      > >
      > >[/color]
      >
      >[/color]


      Comment

      Working...