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 / samples5 / ch14 / fclient.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1997-02-16  |  5.3 KB  |  170 lines

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