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 anot开发者_如何学Cher 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 i.e. 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.
why not you are using xml as a mediator between two application. Use XML for the communication.
If you are gonna use .Net 4.0, you could use Memory Mapped Files.
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
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.
In my case, I had 2 application
- WCF Service based application
- 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.
How about using the SQL Server Compact Edition?
精彩评论