Running a single instance

Author : uwekel
Current version : 16.02.2014
Date of creation : 2014-02-16 20:23:16
Date of last version : 2014-02-16 20:23:16
Compatible with : Linux, Mac, Windows
Compile on : PureBasic only
Basic version used : PureBasic 5.21 LTS
Category : Module,
Description : With this module you can check, whether or not your application is already running. If so, you can send data from the second instance to the first instance. E.g. you could send the ProgramParameter() from the second instance to the first.
  1.  
  2. DeclareModule Instance
  3.  
  4.   Declare Init(Port)
  5.   Declare Send(Text.s)
  6.   Declare Receive(*Text.String)
  7.  
  8. EndDeclareModule
  9.  
  10. Module Instance
  11.   EnableExplicit
  12.  
  13.   #BufferSize = 256
  14.   Global Dim Buffer.a(#BufferSize - 1)
  15.   Global PortNumber
  16.  
  17.   Procedure Init(Port)
  18.     ;network must be available
  19.     If InitNetwork()
  20.       ;keep communication port
  21.       PortNumber = Port
  22.       ;creates a new server listening at the given port
  23.       If CreateNetworkServer(#PB_Any, Port)
  24.         ProcedureReturn #True
  25.       EndIf
  26.     EndIf
  27.   EndProcedure
  28.   Procedure Send(Text.s)
  29.     ;send's a message to a server and return #True on success
  30.     Protected id = OpenNetworkConnection("localhost", PortNumber)
  31.     If id
  32.       SendNetworkString(id, Text)
  33.       CloseNetworkConnection(id)
  34.       ProcedureReturn #True
  35.     EndIf
  36.   EndProcedure
  37.   Procedure Receive(*Text.String)
  38.     ;receives a message from a connected client
  39.     Protected len
  40.     If NetworkServerEvent() = #PB_NetworkEvent_Data
  41.       *Text\s = ""
  42.       Repeat
  43.         len = ReceiveNetworkData(EventClient(), @Buffer(0), #BufferSize)
  44.         If len > 0
  45.           *Text\s + PeekS(@Buffer(0))
  46.         EndIf
  47.       Until len < #BufferSize
  48.       ProcedureReturn #True
  49.     EndIf
  50.   EndProcedure
  51.  
  52. EndModule
  53.  
  54. CompilerIf #PB_Compiler_IsMainFile
  55.   ;check if another instance is already running
  56.   #MyPortNumber = 1249
  57.   If Instance::Init(#MyPortNumber)
  58.     ;this is the first instance
  59.     If OpenWindow(0, 0, 0, 500, 200, "One Instance Only", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  60.       Repeat
  61.         ;run main event loop with timeout value
  62.         Select WaitWindowEvent(100)
  63.         Case #PB_Event_CloseWindow
  64.           Break
  65.         EndSelect
  66.         ;check if another instance sent a string
  67.         If Instance::Receive(arg.String)
  68.           MessageRequester("", arg\s)
  69.         EndIf
  70.       ForEver
  71.     EndIf
  72.   Else
  73.     ;Instance init failed because it has already been inited by the first instance of a program
  74.     ;send something to the first instance (could also be ProgramParameter())
  75.     Instance::Send("Another instance already running")
  76.   EndIf
  77. CompilerEndIf

No comments on this source code.