Transfer large data between .net applications on same computer

后端 未结 4 1503
野性不改
野性不改 2020-12-29 09:03

I have two .net applications that run on the same machine. The first application is the \'Engine\'. It builds images - image\'s size is about 4M. The second applications is

4条回答
  •  清酒与你
    2020-12-29 09:28

    There is number of good options:

    • Message Queue
    • Named Pipes (directly)
    • Memory mapped files
    • WCF on Named Pipes or MSMQ

    Any of those is more than fast enough so I would suggest easiest to implement.

    Message Queue (MSMQ) in my opinion is simplest to use, gives you object transfer (as opposed to streams) and gives you optional transport persistence (useful in case sender or receiver is not running). All this is true for WCF over MSMQ but WCF means more overhead, complexity and configuration involved and no additional (in this case) value.

    Send like this:

    MessageQueue queue = new MessageQueue(".\\private$\\ImagesQueue");
    Message msg = new Message
    {
        Formatter = new BinaryMessageFormatter(),
        Body = myImage,
        Label = "Image"
    };
    queue.Send(msg);
    

    Receive:

    MessageQueue queue = new MessageQueue(".\\private$\\ImagesQueue");
    msg = queue.Receive(TimeSpan.FromMilliseconds(100));
    if (msg != null)
    {
        msg.Formatter = new BinaryMessageFormatter();
        myImage = (MyImage)msg.Body;
    }
    

    Queue needs to be created before use. You can do that when your application starts

    Have this in your class:

    private const string queueName = ".\\private$\\ImagesQueue";
    

    And in application initialization/startup make sure you have your queue:

    if (!MessageQueue.Exists(queueName)
    {
        MessageQueue myQueue = MessageQueue.Create(queueName);
    }
    

    With this queue mechanism, Engine does not have to wait for the Viewer to complete. This would much improve perceived performance because you can generate next image (actually number of them) while previous one is still being viewed. Not so easy to achieve with memory mapped files.

    MSMQ is a standard Windows component but needs to be enabled in Windows Features.

提交回复
热议问题