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#
  4. read and write at the same time

read and write at the same time

Scheduled Pinned Locked Moved C#
data-structurestutorialquestion
7 Posts 3 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.
  • N Offline
    N Offline
    nidhelp
    wrote on last edited by
    #1

    Hi all! i have a read and write function which call from method in other class.. instead of either can read or write at a time,i want to make it both can work at the same time(i want to communicated via serial port).Does anybody know how to do it? using System; using System.Text; namespace yong { public class Class2 { static void Main(string[] args) { // Convert string to byte array and send string x = Console.ReadLine(); byte[] byteDateLine = Encoding.ASCII.GetBytes( x.ToCharArray() ); port CommPort = new port(); CommPort.Open(); CommPort.Write(byteDateLine); //Convert Byte to String //string sBuffer = Encoding.ASCII.GetString(dcbCommPort.Read(10)); //Console.WriteLine( sBuffer.ToString() ); } } }

    C 1 Reply Last reply
    0
    • N nidhelp

      Hi all! i have a read and write function which call from method in other class.. instead of either can read or write at a time,i want to make it both can work at the same time(i want to communicated via serial port).Does anybody know how to do it? using System; using System.Text; namespace yong { public class Class2 { static void Main(string[] args) { // Convert string to byte array and send string x = Console.ReadLine(); byte[] byteDateLine = Encoding.ASCII.GetBytes( x.ToCharArray() ); port CommPort = new port(); CommPort.Open(); CommPort.Write(byteDateLine); //Convert Byte to String //string sBuffer = Encoding.ASCII.GetString(dcbCommPort.Read(10)); //Console.WriteLine( sBuffer.ToString() ); } } }

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      Assuming hte port allows it, try running the methods on different threads. Otherwise, each method will only be called when the last method ends. Why do you need them to be intermixed ? I suspect the end result will be the same, in terms of speed, unless you're on a multi CPU machine. Christian Graus - Microsoft MVP - C++

      L 1 Reply Last reply
      0
      • C Christian Graus

        Assuming hte port allows it, try running the methods on different threads. Otherwise, each method will only be called when the last method ends. Why do you need them to be intermixed ? I suspect the end result will be the same, in terms of speed, unless you're on a multi CPU machine. Christian Graus - Microsoft MVP - C++

        L Offline
        L Offline
        levis
        wrote on last edited by
        #3

        I am not so familliar with threads methods.May you try to edit for me?thank a lot!How about i use While loop and at the same time inside the While loop contain a if-else loop?

        C 1 Reply Last reply
        0
        • L levis

          I am not so familliar with threads methods.May you try to edit for me?thank a lot!How about i use While loop and at the same time inside the While loop contain a if-else loop?

          C Offline
          C Offline
          Christian Graus
          wrote on last edited by
          #4

          levi`s wrote: I am not so familliar with threads methods.May you try to edit for me? MSDN[^] has plenty of example code. levi`s wrote: How about i use While loop and at the same time inside the While loop contain a if-else loop? Well, they would still be on the same thread, but if you put the write and read code in the same loop, that would make them both happen together, but I don't see how it would get faster as a result. Christian Graus - Microsoft MVP - C++

          L 1 Reply Last reply
          0
          • C Christian Graus

            levi`s wrote: I am not so familliar with threads methods.May you try to edit for me? MSDN[^] has plenty of example code. levi`s wrote: How about i use While loop and at the same time inside the While loop contain a if-else loop? Well, they would still be on the same thread, but if you put the write and read code in the same loop, that would make them both happen together, but I don't see how it would get faster as a result. Christian Graus - Microsoft MVP - C++

            L Offline
            L Offline
            levis
            wrote on last edited by
            #5

            thanks for your advice..i think using thread is better...may you help me to edit my source code using thread?i am just a very new beginner in c#..I need your help a lot.Thank so much!

            C 1 Reply Last reply
            0
            • L levis

              thanks for your advice..i think using thread is better...may you help me to edit my source code using thread?i am just a very new beginner in c#..I need your help a lot.Thank so much!

              C Offline
              C Offline
              Christian Graus
              wrote on last edited by
              #6

              Using threads is cleaner, but the code is a lot harder to debug, and I doubt it will be any faster, it will just be interspersed, which seems to be what you're hoping for. If you read the MSDN articles, they should give you a good start. Whatever you try, just post your code here if you get stuck ( i.e. as a new question ) and I'd be happy to help. Christian Graus - Microsoft MVP - C++

              L 1 Reply Last reply
              0
              • C Christian Graus

                Using threads is cleaner, but the code is a lot harder to debug, and I doubt it will be any faster, it will just be interspersed, which seems to be what you're hoping for. If you read the MSDN articles, they should give you a good start. Whatever you try, just post your code here if you get stuck ( i.e. as a new question ) and I'd be happy to help. Christian Graus - Microsoft MVP - C++

                L Offline
                L Offline
                levis
                wrote on last edited by
                #7

                ok thanks!i will try to do it!

                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