LOTUSSCRIPT 言語


拡張例:lock 関数
次の一連の例は、Web サイトの「ヒット」数 (Web サイトの訪問回数) を累積する場合の、Lock 関数の使い方を示しています。最初の例で、複数の人が同時に同じ Web サイトをヒットした場合にどうなるかを示しています。ユーザーにはまったく同じ数字が表示され、増分はオフになります。

例 1:

Sub Initialize

Dim Sess As New NotesSession

Dim Doc As NotesDocument

Dim Count As NotesItem

Set Doc = Sess.SavedData

Set count = Doc.GetFirstItem("WebHits")

If count Is Nothing Then

Set count = New NotesItem(Doc, "WebHits", 0)

End If

count.Values = count.Values(0) + 1

Call Doc.Save(True,False)

End Sub

2 番目の例は、例 1 で示された問題を CodeLock を使用して回避する方法について示しています。安全なロックを作成して確認した後、カウントを読み取って変更します。作業が終わったらこのロックを解除します。

例 2:

Sub Initialize

Dim Sess As New NotesSession

Dim Doc As NotesDocument

Dim Count As NotesItem

Dim Status As Integer

Dim LockID As Integer

Dim others As Integer

'  Creating a Lock ID or getting the Lock ID

'  For the event of "WebSiteHits"

LockID = Createlock("WebSiteHits")

'  Infinite loop that can only be exited

'  when this agent has a successfull

'  lock.An unsuccessfull lock means

'  that this agent is presently being

'  run by someone else.

Do While True

If Codelock(LockID) Then

Exit Do'  We finally have a lock, exiting Loop

End If

Loop

Set Doc = Sess.SavedData

Set count = Doc.GetFirstItem("WebHits")

If count Is Nothing Then

Set count = New NotesItem(Doc, "WebHits", 0)

End If

count.Values = count.Values(0) + 1

Call Doc.Save(True,False)

'  Once completed, release and

'  destroy this lock so another

'  run of this agent can continue.

Status = CodeUnlock(LockID)

Status = DestroyLock(LockID)

End Sub