1

This is possible duplicate. But, I couldn't able to find a prompt solution. Please help me out.

I have library project which has a class called GetData(ByVal contextId as Integer) function to get data from database and stores them into shared variable called Public Shared _ContextData As String. And, this library project referred by three window applications.

If all my three windows applications run simultaneously and try to get data with different contextId from database using the above method in that library.

Is there possible cause to share any one's contextId values across application?

I have referred below links already. But, they were telling the solution for web application.

Link 1

Link 2

Community
  • 1
  • 1
Balagurunathan Marimuthu
  • 2,927
  • 4
  • 31
  • 44

2 Answers2

2

No, the Shared variables are bound to the ApplicationDomain (usually there is one per process). So in your case, you'd have a separate variable in memory for each process of your windows applications. If you run each of your windows applications, you'd have 3, if you run two instances of each of your windows applications, you'd have 6 separate variables.

If you would like to share the value across process boundaries, you'd have to look into cross process communication technologies like WCF or Named Pipes communication. Also, creating a service is an option.

A simpler option is to store the value in a database or in a file that each of the applications accesses. In either of these options, you'd need to think about how to handle concurrency situations when two applications access the value at the same time.

If the contextId does not change once it is selected, an option is to have a main application and choose the contextId in this application. You'd start the other applications from this main application (e.g. using Process.Start) and hand over the contextId as a command line argument. These "child"-applications use the contextId that is given to them at startup.

Markus
  • 20,838
  • 4
  • 31
  • 55
2

Shared is only shared per application and not all applications, I'm afraid. When loading a DLL you load it into the application's memory, thus each application has its own instance of your library loaded into the application memory (which is why Shared doesn't work cross-application wise).

If you want to share data across many applications you should look into Memory Mapped Files.

Visual Vincent
  • 18,045
  • 5
  • 28
  • 75