Program 1 time starting

General FreeBASIC programming questions.
Post Reply
Jawade
Posts: 228
Joined: Apr 25, 2008 19:13

Program 1 time starting

Post by Jawade »

What is the best way to get a program not starting more than 1 time?
Jawade
Posts: 228
Joined: Apr 25, 2008 19:13

Re: Program 1 time starting

Post by Jawade »

I mean at the same time.
badidea
Posts: 2591
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: Program 1 time starting

Post by badidea »

Interesting question. Two things came to my mind:
A) Create/lock a file. If file exists or is locked, another instance of the program is running.
B) Check if the application exists in memory. Some OS calls are needed for this which probably makes it OS dependent (Windows/Linux).
A quick search gave me similar answers: Determine programmatically if a program is running
For Windows this popped up: Run single instance of an application using Mutex. But this is beyond my knowledge.

Edit: The same question in 2006: Mutex instances and related: Mutex Troubles [Solved]
Last edited by badidea on Nov 11, 2018 22:25, edited 1 time in total.
St_W
Posts: 1626
Joined: Feb 11, 2009 14:24
Location: Austria
Contact:

Re: Program 1 time starting

Post by St_W »

On Windows this is usually done using a named Mutex, as described in this article by Microsoft:
https://support.microsoft.com/en-us/hel ... n-visual-c
Jawade
Posts: 228
Joined: Apr 25, 2008 19:13

Re: Program 1 time starting

Post by Jawade »

Tjanks for the reply's. I have made one by myself but I don't know if it is a good one:

Code: Select all

''Test if the program already is started

Dim aa As String
Dim tst1 As String
Dim tst2 As String

''This part at the begin of your program
Kill "existing.tst": Sleep 1200
Open "existing.tst" For Input As #1
    Line Input #1, tst1
Close #1
If tst1 = "arrow" Then System
Open "existing.tst" For Output As #1
    Print #1, "arrow"
Close #1

''Current loop in an existing program
Print "Press any key"
Do
    Do
        Print Timer; ' A function of the program
        aa = InKey$: Sleep 3
        If tst2 <> Left$(Time$, 8) Then
            tst2 = Left$(Time$, 8)
            Open "existing.tst" For Input As #1: Close #1
                If Err Then
                    Open "existing.tst" For Output As #1
                    Print #1, "arrow"
                End If
            Close #1
        End If
    Loop Until aa <> ""
Loop Until InKey$ = Chr$(27)
Jawade
Posts: 228
Joined: Apr 25, 2008 19:13

Re: Program 1 time starting

Post by Jawade »

Code: Select all

''A better way?

''Test if the program already is started

Dim aa As String
Dim tst1 As String

''This part at the begin of your program
Open "existing.tst" For Input As #1
    Line Input #1, tst1
Close #1
If tst1 = "arrow" Then System
Open "existing.tst" For Output As #1
    Print #1, "arrow"
Close #1

''Current loop in an existing program
Do
    Do
        Print Timer; ''An example function of the program
        aa = InKey$: Sleep 3
    Loop Until aa <> ""
Loop Until aa = Chr$(27)
Kill "existing.tst" ''This at the end of your program
End
Post Reply