简体   繁体   中英

How to set variable in second process from first process

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 (eg 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, eg IPC, shared memory, named pipes....

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

Thanks a lot and with best regards,

Matthias

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 !!!

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