Multi-threaded pipeline communication on windows

Source: Internet
Author: User
Tags readfile

A pipeline is actually a piece of shared memory, which has two ends that are used for both process reads and writes. Here's how to implement pipeline communication between threads on Windows.

Reference Original: Multithreaded Pipe communication on Windows

C # multithreaded Pipeline communication

Create a Pipeline instance:

INTPTR pipe = PipeCommunication.Pipe.CreateNamedPipe (Pipe_name, (UINT) Pipecommunication.pi Peopenmodeflags.pipe_access_duplex, (UINT) (PipeCommunication.PipeModeFlags.PIPE_TYPE_BYTE | PipeCommunication.PipeModeFlags.PIPE_READMODE_BYTE), 1, 0, IntPtr.Zero);

Wait for the client to connect:

PipeCommunication.Pipe.ConnectNamedPipe (Pipe, ref nativeoverlapped);

Client Connection Pipeline Service side:

INTPTR pipe = PipeCommunication.Pipe.CreateFile (Pipe_name, (UINT) (Pipecommunication.desirem Ode. Generic_read | PipeCommunication.DesireMode.GENERIC_WRITE), 0, IntPtr.Zero, 3, IntPtr.Zero);

Read and write data between threads:

Service side

PipeCommunication.Pipe.WriteFile (pipe, bytes,  (UINT) (sizeof (byte)  * bytes. Length),  byteswrittenorred, ref nativeoverlapped);             try            {                 while  ( VALUE < 10)                  {                     //omit to Clear bytes                     pipecommunication.pipe.readfile (Pipe,  bytes,  (UINT) (sizeof (byte)  * bytes. Length),  byteswrittenorred, ref nativeoverlapped);                    value =  Bitconverter.toint32 (bytes, 0);                     value++;                     bytes = bitconverter.getbytes ( Value);                     thread.sleep (;            )         pipecommunication.pipe.writefile (pipe, bytes,  (UINT) (sizeof (byte)  * bytes. Length),  byteswrittenorred, ref nativeoverlapped);                 }             } 

Client

try            {                 while  (value < 10)                  {                     //omit  to Clear bytes                     pipecommunication.pipe.readfile (pipe, bytes,  (UINT) (sizeof (byte)  * bytes. Length),  byteswrittenorred, ref nativeoverlapped);                     value =  Bitconverter.toint32 (bytes, 0);                     value++;                     bytes = bitconverter.getbytes (value);                      thread.sleep (;              )       pipecommunication.pipe.writefile (pipe, bytes,  (UINT) (sizeof (byte )  * bytes. Length),  byteswrittenorred, ref nativeoverlapped);                 }             }
Source

Https://github.com/DynamsoftRD/windows-pipe-communication

git clone https://github.com/DynamsoftRD/windows-pipe-communication.git
Reference

Pipes

Multi-threaded pipeline communication on windows

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.