home *** CD-ROM | disk | FTP | other *** search
/ Dan Appleman's Visual Bas…s Guide to the Win32 API / Dan.Applmans.Visual.Basic.5.0.Programmers.Guide.To.The.Win32.API.1997.Ziff-Davis.Press.CD / VB5PG32.mdf / vbpg32 / samples4 / ch14 / fclient.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1997-02-16  |  5.3 KB  |  171 lines

  1. VERSION 4.00
  2. Begin VB.Form frmClient 
  3.    Caption         =   "Customer"
  4.    ClientHeight    =   3000
  5.    ClientLeft      =   840
  6.    ClientTop       =   1785
  7.    ClientWidth     =   4470
  8.    Height          =   3405
  9.    Left            =   780
  10.    LinkTopic       =   "Form1"
  11.    ScaleHeight     =   3000
  12.    ScaleWidth      =   4470
  13.    Top             =   1440
  14.    Width           =   4590
  15.    Begin VB.Timer Timer1 
  16.       Interval        =   500
  17.       Left            =   3360
  18.       Top             =   240
  19.    End
  20.    Begin VB.ListBox lstPurchase 
  21.       Height          =   1395
  22.       Left            =   180
  23.       TabIndex        =   3
  24.       Top             =   1140
  25.       Width           =   4095
  26.    End
  27.    Begin VB.CheckBox chkShop 
  28.       Caption         =   "Shop"
  29.       Height          =   255
  30.       Left            =   1260
  31.       TabIndex        =   2
  32.       Top             =   660
  33.       Width           =   1335
  34.    End
  35.    Begin VB.TextBox txtClient 
  36.       Height          =   285
  37.       Left            =   1260
  38.       MaxLength       =   1
  39.       TabIndex        =   0
  40.       Text            =   "1"
  41.       Top             =   240
  42.       Width           =   1335
  43.    End
  44.    Begin VB.Label lblStatus 
  45.       Height          =   195
  46.       Left            =   180
  47.       TabIndex        =   4
  48.       Top             =   2700
  49.       Width           =   4095
  50.    End
  51.    Begin VB.Label Label1 
  52.       Alignment       =   1  'Right Justify
  53.       Caption         =   "Customer #:"
  54.       Height          =   195
  55.       Left            =   120
  56.       TabIndex        =   1
  57.       Top             =   300
  58.       Width           =   1095
  59.    End
  60. Attribute VB_Name = "frmClient"
  61. Attribute VB_Creatable = False
  62. Attribute VB_Exposed = False
  63. Option Explicit
  64. ' Copyright 
  65.  1997 by Desaware Inc. All Rights Reserved
  66. ' Simple state machine
  67. ' 0 - Idle
  68. ' 1 - Looking for checkstand
  69. ' 2 - Waiting for checkstand to be free
  70. ' 3 - Checkstand is free, data loaded
  71. ' 4 - Waiting for checkout complete
  72. Dim CurrentState%
  73. Dim CurrentTotal As Single
  74. Dim MappingHandle As Long
  75. Dim MappingAddress As Long
  76. Dim MutexHandle As Long
  77. Private Sub Form_Unload(Cancel As Integer)
  78.     CleanUp
  79. End Sub
  80. Private Sub Timer1_Timer()
  81.     Dim usename$
  82.     Dim newtotal&
  83.     Dim cs As CheckStand
  84.     Dim x%
  85.     Dim res&
  86.     Select Case CurrentState
  87.         Case 0
  88.             If chkShop.value = 1 Then
  89.                 CurrentState = 1
  90.             End If
  91.         Case 1
  92.             usename$ = GetMappingName$()
  93.             ' Right now we only use checkstand 1
  94.             MappingHandle = OpenFileMapping(FILE_MAP_WRITE, False, usename$)
  95.             MutexHandle = OpenMutex(MUTEX_ALL_ACCESS, False, GetMutexName$())
  96.             If MappingHandle = 0 Or MutexHandle = 0 Then Exit Sub
  97.             MappingAddress = MapViewOfFile(MappingHandle, FILE_MAP_ALL_ACCESS, 0, 0, 0)
  98.             CurrentState = 2
  99.         Case 2
  100.             ' Obtain exclusive ownership
  101.             res = WaitForSingleObject(MutexHandle, 0)
  102.             If res <> WAIT_TIMEOUT Then
  103.                 agCopyData ByVal MappingAddress, cs, Len(cs)
  104.                 If cs.Total = 0 Then CurrentState = 3 Else ReleaseMutex (MutexHandle)
  105.             End If
  106.         Case 3
  107.             CurrentTotal = 0
  108.             For x = 0 To CInt(Rnd(99))
  109.                 cs.Prices(x) = 100 * Rnd()
  110.                 CurrentTotal = CurrentTotal + cs.Prices(x)
  111.             Next x
  112.             cs.Done = True
  113.             cs.Client = "Client" & txtClient.Text
  114.             agCopyData cs, ByVal MappingAddress, Len(cs)
  115.             CurrentState = 4
  116.         Case 4
  117.             agCopyData ByVal MappingAddress, cs, Len(cs)
  118.             If cs.Total <> 0 Then
  119.                 For x = 0 To 99
  120.                     newtotal = newtotal + cs.Prices(x)
  121.                 Next x
  122.                 cs.Total = 0
  123.                 lstPurchase.AddItem Format$(CurrentTotal, "0.00")
  124.                 agCopyData cs, ByVal MappingAddress, Len(cs)
  125.                 ' Release the mutex
  126.                 Call ReleaseMutex(MutexHandle)
  127.                 If chkShop.value = 0 Then
  128.                     CurrentState = 0
  129.                 Else
  130.                     CurrentState = 1
  131.                 End If
  132.             ' Clear file mapping handles, etc.
  133.             CleanUp
  134.             End If
  135.     End Select
  136.     Select Case CurrentState
  137.         Case 0
  138.             lblStatus.Caption = "Idle"
  139.         Case 1
  140.             lblStatus.Caption = "Looking for checkstand"
  141.         Case 2
  142.             lblStatus.Caption = "Waiting in line at checkstand"
  143.         Case 3
  144.             lblStatus.Caption = "Loading items onto checkstand"
  145.         Case 4
  146.             lblStatus.Caption = "Waiting for checkout to be complete"
  147.     End Select
  148. End Sub
  149. Public Function GetMappingName() As String
  150.     GetMappingName = "ChkStd1map"
  151. End Function
  152. Public Function GetMutexName() As String
  153.     GetMutexName = "ChkStd1mutex"
  154. End Function
  155. Private Sub CleanUp()
  156.     ' Remember, this won't get called if you Stop without
  157.     ' closing the main window.
  158.     If MappingAddress <> 0 Then
  159.         Call UnmapViewOfFile(MappingAddress)
  160.         MappingAddress = 0
  161.     End If
  162.     If MappingHandle <> 0 Then
  163.         Call CloseHandle(MappingHandle)
  164.         MappingHandle = 0
  165.     End If
  166.     If MutexHandle <> 0 Then
  167.         Call CloseHandle(MutexHandle)
  168.         MutexHandle = 0
  169.     End If
  170. End Sub
  171.