Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

pyqt send message to another instance

from PyQt5.QtCore import QSharedMemory, QTimer, QObject, pyqtSignal
from PyQt5.QtWidgets import QMessageBox, QWidget, QApplication

class SingleInstanceException(Exception):
    pass

class SharedMemoryReader(QObject):

    received = pyqtSignal(bytes)

    def __init__(self, key, parent = None):
        super().__init__(parent)

        memory = QSharedMemory(key)
        if not memory.create(1, QSharedMemory.ReadWrite):
            raise SingleInstanceException()
        
        self._memory = memory
        memory.lock()
        self.clearMemory()
        memory.unlock()

        # timer to poll shared memory for changes
        timer = QTimer()
        timer.timeout.connect(self.onTimeOut)
        timer.start(500)
        self._timer = timer

    def clearMemory(self):
        self._memory.data()[0] = b'0'
        
    def onTimeOut(self):
        memory = self._memory
        memory.lock()
        data = memory.data()[0]
        if data != b'0':
            self.received.emit(data)
            self.clearMemory()
        memory.unlock()

class SharedMemoryWriter(QObject):

    def __init__(self, key, parent = None):
        super().__init__(parent)
        memory = QSharedMemory(key)
        self._memory = memory
        
    def write(self, message):
        memory = self._memory
        if not memory.isAttached():
            memory.attach()
        memory.lock()
        memory.data()[0] = message
        memory.unlock()

if __name__ == "__main__":
    app = QApplication([])

    # guid to identify your application (some unique string)
    guid = "5c197822-e86a-4547-a4f1-dbb5087b962d"

    widget = QWidget()

    try:
        # First instance creates shared memory. 
        # If memory exists exception raised and it means that it's not first instance.
        reader = SharedMemoryReader(guid)
        reader.received.connect(lambda: QMessageBox.information(widget,"","Signal from another instance received"))
    except SingleInstanceException:
        # Second instance writes to memory (sends signal to first instance) and exits
        writer = SharedMemoryWriter(guid)
        writer.write(b'1')
        exit(0)
    
    # No exception raised - that means its first instance of application
    # Continue normally
    widget.show()

    app.exec_()
Comment

PREVIOUS NEXT
Code Example
Csharp :: Toggle value change 
Csharp :: .net objects 
Csharp :: wpf ope another project page 
Csharp :: Include multiple siblings at the Level 
Csharp :: C# milisecond to h m s 
Csharp :: c# function to validate decimal upto p(25,2) 
Csharp :: get datacontext of itemscontrol item c# 
Csharp :: c# generate random date of birth but over 18 
Csharp :: c# entity framework order by array 
Csharp :: C# return dictionary string/integer from comparison of List and Array 
Csharp :: how to show enum name list from input in swagger c# 
Csharp :: principalcontext c# example 
Csharp :: C# Relational Operators 
Csharp :: c# loop datatable column names convert to list 
Csharp :: creating an object 
Csharp :: sequelize instance method is not a function 
Csharp :: c# regex double of some letters only 
Csharp :: Getting the ID of the element that fired an event 
Csharp :: my context class is in different project and i want migration in different project in asp.net mvc 
Csharp :: tmpro pageCount update 
Csharp :: how to do if statement based on date in asp net c# 
Csharp :: get the next letter after specific character in c# 
Csharp :: wpf change the content of the button wait 5 secound and then change it again 
Csharp :: wpf onpropertychanged not working 
Csharp :: Game of two stack c# 
Csharp :: VideoPlayer.isPlaying 
Csharp :: c# webrtc dll 
Csharp :: CS0176 
Csharp :: player not following slide object unity 2d 
Csharp :: remove language folders build visual studio 
ADD CONTENT
Topic
Content
Source link
Name
5+1 =