0

first of all: I'm a newbie in this forum and not a professional programmer, just hobby.

This is what I have: One solution in VisualStudio with 3 Projects in VB.net. First project contains common functions etc. Second project is a windows service. Third project is a Windows Forms UI.

Second an third project imports the first project.

My problem: When the service from second project is running and UI from third project is started I want to set a variable (e.g. by pressing a button) that will be set in the service, too. So the service is informed to do some special things.

I've tried to declare this variable in the common project, but this doesn't work. After searching a bit I know now this can't work, because the service and the UI are seperate processes.

There are several solutions to communicate between processes, e.g. IPC, shared memory, named pipes....

But isn't there a simple way to solve my problem ?

Thanks a lot and with best regards,

Matthias

  • Obviously there are also files and databases, but you want a direct process to process communication? – Steve Mar 21 '21 at 10:08
  • There might not be an easier way but a shady workaround would be to write the variable to a file and reading it from the second process. – preciousbetine Mar 21 '21 at 10:10
  • @ Steve: yes, there are files (.ini file). But I don't want to read this file from service cyclical. I want to tell the service the new value direct from UI. – Schlumpf901 Mar 21 '21 at 10:44
  • Yeah, of course there's a simple solution. No one talks about it on the internet though, because they prefer people to use difficult solutions instead. I'm joking, of course. If there was a simple solution then that's what you would have found first when you searched because that's what everyone would be using. – jmcilhinney Mar 21 '21 at 11:32
  • It's not so unusual to have to restart a service when there is a change in configuration, so I don't think your goal is necessarily common or easy to do. – Craig Mar 22 '21 at 13:31

1 Answers1

0

OK, thanks for your answers!! :-) I decided to try it with memory mapped file.

This is my making function:

Public Function MakeMemoryMappedFile(ByVal pValue As String) As String

    Dim Buffer As Byte() = ASCIIEncoding.ASCII.GetBytes(pValue)

    Try
        Dim mmf As MemoryMappedFile = MemoryMappedFile.CreateOrOpen("test", 10000)
        Dim accessor As MemoryMappedViewAccessor = mmf.CreateViewAccessor()
        accessor.Write(54, CType(Buffer.Length, UShort))
        accessor.WriteArray(54 + 2, Buffer, 0, Buffer.Length)

        Return "ok"

    Catch ex As Exception

        Return "Error = " & ex.Message

    End Try

End Function

And this is my reading function:

Public Function ReadMemoryMappedFile() As String

    Try
        Dim mmf As MemoryMappedFile = MemoryMappedFile.OpenExisting("test")
        Dim accessor As MemoryMappedViewAccessor = mmf.CreateViewAccessor()
        Dim Size As UShort = accessor.ReadUInt16(54)
        Dim Buffer As Byte() = New Byte(Size - 1) {}
        accessor.ReadArray(54 + 2, Buffer, 0, Buffer.Length)
        Return ASCIIEncoding.ASCII.GetString(Buffer)

    Catch noFile As FileNotFoundException

        Return "No File found ..."

    Catch ex As Exception

        Return "Error = " & ex.Message

    End Try
End Function

I have on my application a button for setting the value and one button for reading the value -> works fine.

If I start this application twice and set the value in the first instance and then read the value from the second instance -> it works fine.

But: If I set the value in the application my windows service can't read ist. Always got a "FileNotFoundException".

Could you please tell me whats wrong ?!?

Thanks !!!

  • Got it ! In this post https://stackoverflow.com/questions/59977590/using-memory-maps-with-a-service is a hint for the Global Mapped File and now it works.... :-) – Schlumpf901 Mar 25 '21 at 13:04