简体   繁体   中英

Sharing data between two applications

like this

public class MyClass { 
  public static instance = new MyClass();
 private List<int> idList; 
}

. I am using this class in two different window application. like this MyClass.instance.IdList.Add(1); All data in idList i am storing in file and fetching info from that file. I am adding value to idList in one app and I am fetching idList info in another app. but it is not showing idList content in second application which is added by first apllication. How to achive this?

Here are a few ways to share data between applications:

WCF would be my preferred way to do this and I will add some explanation here

1) Use of WCF - Host a WCF service with following functionality [Effort: Moderate]

//Notice the use of Publisher -Subscriber pattern here (each app will subscribe to the service, the service could be hosted by all apps at certain endpoint ie net.pipe://localhost/NotificationService (Since multiple applications will try to host the same service only one would succeed and that's exactly what we want)

void Subscribe(object);
void Unsubscribe(object);

// Any client wanting to add an object to the list will call Add

void Add(object objectToAdd);

// Iterate through each subscribing app and send a notification that list changed

void Notify();

// Return the current state of list

IEnumerable<object> GetYourList();

2) Use of Clipboard [Effort: Simple]

3) Use of File system & listening to the file change notification [Effort: Simple]

4) Memory Mapped Files [Effort: Simple-Moderate]

Maybe Remoting helps you. It allows you to let other applications access functions in your server application over the internet (or a local network). So the code is executed on the server application. Here is an example: http://www.codeproject.com/KB/IP/Net_Remoting.aspx

Maybe you are aware of this already, but types declared static are local to a single process (application). Reading your example it appears that you are expecting the static instance to be shared and accessible between applications. This is not the case.

A standard way to do what you need is to create a 3rd application/process that exposes synchronised access to a list that both of the other applications can add/remove items from.

如果您要使用.Net 4.0,则可以使用内存映射文件

如何使用SQL Server Compact Edition?

why not you are using xml as a mediator between two application. Use XML for the communication.

I think this is the prefect candidate for a Message Queue

With the message queue, you can add IDs to the queue in one app, and pull them off in another.

This page gives a good example of how to implement queues: Use Microsoft Message Queuing in C# for inter-process communication

The MSDN gives an example service here: C#: A Message Queuing Service Application

In my case, I had 2 application

  1. WCF Service based application
  2. Windows application

I had used Message Queue to overcome this data sharing issue

  • Add how much ever queue you want.
  • Its a blocking read, hence your application doesn't overrun and hog the CPU.
  • Pretty reliable, no Data loss.
  • Add Header along with Each data, to recognize it what type of data is this.

How to use :

  • Create private queues.
  • push data into it from client side.
  • another side read and parse it.

Message Queue is the best way of communicating. However cheque that you need to enable the "Microsoft Message Queue" component before you can use the service.

Further, we can get real time data exchange as it also creates event, if configured to do so. and FIFO way of accessing the message will enable to process each and every message without missing any single message.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM