Share via

How do I share specific pieces of information between related apps or multiple instances of one app?

Robert Gustafson 611 Reputation points
2021-10-19T02:01:51.73+00:00

WHAT I HAVE:
Visual Basic 2019, WinForms, .NET Framework 4.6-4.8

MY ISSUE:
I want to structure a desktop app so that it can share specific information with related desktop apps--or with other instances of itself--when they're running at the same time. Each app or app instance would have its own instance of an object of some type, say MyClass,--which the relate apps or other app instances could look at to compare with their own corresponding MyClass instances.

For instance, an application that can be multiply-instanced might want to have, say, a file-name String that other instances of itself could see, so that the app can make sure that simultaneously-running instances aren't using and modifying the same file, the same document, the same picture, the same database, or the same whatever at the same time. Ideally, the ability to share and compare info should be a simple object-oriented thing that doesn't require me to explicitly set up a common file or common database simply for a relatively small amount of "state-info" for each app or app instance. (That seems like overkill!)

Give me some advice ASAP, please provide any code in VB.NET, and keep it as simple as possible, as I don't want to devote a large part of app development simply for the sharing and comparing of a relatively limited bit of data between a program and other programs or instances of itself.

Developer technologies | Windows Forms
Developer technologies | VB
{count} votes

12 answers

Sort by: Most helpful
  1. Robert Gustafson 611 Reputation points
    2021-11-07T21:31:13.463+00:00

    I need my Memory Mapped File to exist so long as ANY instances of my app are running at a given time, with the MMF disappeared only when no application instances are running. And it can't matter what the order of the instances closing is compared to the order of them opening. I imagine an MMF effectively with array of InstanceInfo, with InstanceInfo so defined:

    <StructLayout(LayoutKind.Sequential, CharSet:= CharSet.Unicode> _
    Public Class InstanceInfo
       InstanceId As Integer 'process Id of an instance
       <MarshalAs(UnmangedType.ByValTStr, SizeConst:= 260> _
       InstanceFile As String 'file being used by instance
    End With
    

    The MMF array gets searched to see if any items contain an InstanceFile value identical to a file about to be opened. When a file is actually opened in the current instance, a new InstanceInfo item is added to the MMF array (with .InstanceId = Process.GetCurrentProcess.Id and .InstanceFile = FileName); when the file is closed, the item is removed from the MMF. Only when all application instances are closed should the MMF be deleted.

    0 comments No comments

  2. Robert Gustafson 611 Reputation points
    2021-11-06T23:34:56.783+00:00

    Where do I get the PID? And how easy/difficult is it the memory-map a group (say, a Dictionary) of data-type values? (Say, a memory-mapped Dictionary with PID as Key and a SomeType-type data as Value?)

    <Serializable()> _
    Public Class *SomeType*
       Public Property Field1 As *Field1Type*
       Public Property Field2 As *Field2Type*
       ...
    End Class
    

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.