home *** CD-ROM | disk | FTP | other *** search
/ Planet Source Code Jumbo …e CD Visual Basic 1 to 7 / 1_2002.ISO / Data / Zips / MThreadVB_882945292002.psc / Demo / Demo.frm (.txt) next >
Encoding:
Visual Basic Form  |  2002-04-29  |  14.6 KB  |  422 lines

  1. VERSION 5.00
  2. Begin VB.Form Form1 
  3.    AutoRedraw      =   -1  'True
  4.    BorderStyle     =   1  'Fixed Single
  5.    Caption         =   "MThreadVB Multithreader For VB - Prime Finder Demo Application"
  6.    ClientHeight    =   6600
  7.    ClientLeft      =   45
  8.    ClientTop       =   330
  9.    ClientWidth     =   7650
  10.    FillStyle       =   0  'Solid
  11.    LinkTopic       =   "Form1"
  12.    MaxButton       =   0   'False
  13.    ScaleHeight     =   6600
  14.    ScaleWidth      =   7650
  15.    StartUpPosition =   2  'CenterScreen
  16.    Begin VB.CommandButton ShowModalForm 
  17.       Caption         =   "Show &Modal Form"
  18.       Height          =   345
  19.       Left            =   3045
  20.       TabIndex        =   19
  21.       Top             =   5370
  22.       Width           =   3345
  23.    End
  24.    Begin VB.CommandButton ShowForm 
  25.       Caption         =   "Show &Non Modal Form from Thread"
  26.       Height          =   345
  27.       Left            =   -15
  28.       TabIndex        =   18
  29.       Top             =   5370
  30.       Width           =   2955
  31.    End
  32.    Begin VB.CommandButton VDlg 
  33.       Caption         =   "&Show voting Dialog..."
  34.       Height          =   390
  35.       Left            =   5730
  36.       TabIndex        =   17
  37.       Top             =   4425
  38.       Width           =   1935
  39.    End
  40.    Begin VB.CommandButton Command1 
  41.       Caption         =   "Clear Log"
  42.       Height          =   330
  43.       Left            =   45
  44.       TabIndex        =   14
  45.       Top             =   4395
  46.       Width           =   1845
  47.    End
  48.    Begin VB.Frame Frame4 
  49.       Caption         =   "Primes Found"
  50.       Height          =   2040
  51.       Left            =   5745
  52.       TabIndex        =   12
  53.       Top             =   2295
  54.       Width           =   1920
  55.       Begin VB.TextBox PText 
  56.          BackColor       =   &H00000000&
  57.          BeginProperty Font 
  58.             Name            =   "MS Sans Serif"
  59.             Size            =   8.25
  60.             Charset         =   0
  61.             Weight          =   700
  62.             Underline       =   0   'False
  63.             Italic          =   0   'False
  64.             Strikethrough   =   0   'False
  65.          EndProperty
  66.          ForeColor       =   &H00C0FFFF&
  67.          Height          =   1740
  68.          Left            =   120
  69.          MultiLine       =   -1  'True
  70.          ScrollBars      =   3  'Both
  71.          TabIndex        =   13
  72.          Top             =   225
  73.          Width           =   1740
  74.       End
  75.    End
  76.    Begin VB.Frame Frame3 
  77.       Caption         =   "Event log"
  78.       Height          =   2040
  79.       Left            =   60
  80.       TabIndex        =   10
  81.       Top             =   2295
  82.       Width           =   5250
  83.       Begin VB.TextBox ELog 
  84.          BackColor       =   &H00000000&
  85.          BeginProperty Font 
  86.             Name            =   "MS Sans Serif"
  87.             Size            =   8.25
  88.             Charset         =   0
  89.             Weight          =   700
  90.             Underline       =   0   'False
  91.             Italic          =   0   'False
  92.             Strikethrough   =   0   'False
  93.          EndProperty
  94.          ForeColor       =   &H00C0FFFF&
  95.          Height          =   1695
  96.          Left            =   105
  97.          MultiLine       =   -1  'True
  98.          ScrollBars      =   3  'Both
  99.          TabIndex        =   11
  100.          Top             =   240
  101.          Width           =   5085
  102.       End
  103.    End
  104.    Begin VB.CommandButton EndThread 
  105.       Caption         =   "Terminate Prime Finder Thread..."
  106.       Enabled         =   0   'False
  107.       Height          =   345
  108.       Left            =   5100
  109.       TabIndex        =   9
  110.       Top             =   1950
  111.       Width           =   2520
  112.    End
  113.    Begin VB.CommandButton StartThread 
  114.       Caption         =   "Start Prime Finder Thread..."
  115.       Height          =   360
  116.       Left            =   2700
  117.       TabIndex        =   8
  118.       Top             =   1935
  119.       Width           =   2265
  120.    End
  121.    Begin VB.Frame Frame2 
  122.       Caption         =   "Parameters"
  123.       Height          =   690
  124.       Left            =   30
  125.       TabIndex        =   5
  126.       Top             =   1260
  127.       Width           =   7590
  128.       Begin VB.ComboBox Pr 
  129.          Height          =   315
  130.          Left            =   690
  131.          Style           =   2  'Dropdown List
  132.          TabIndex        =   6
  133.          Top             =   255
  134.          Width           =   4005
  135.       End
  136.       Begin VB.Label Label3 
  137.          Caption         =   "Priority:"
  138.          Height          =   180
  139.          Left            =   135
  140.          TabIndex        =   7
  141.          Top             =   315
  142.          Width           =   720
  143.       End
  144.    End
  145.    Begin VB.Frame Frame1 
  146.       Caption         =   "Find Primes:"
  147.       Height          =   720
  148.       Left            =   30
  149.       TabIndex        =   0
  150.       Top             =   60
  151.       Width           =   7575
  152.       Begin VB.TextBox EndBox 
  153.          Height          =   285
  154.          Left            =   3945
  155.          TabIndex        =   4
  156.          Text            =   "5000"
  157.          Top             =   240
  158.          Width           =   3255
  159.       End
  160.       Begin VB.TextBox Start 
  161.          Height          =   285
  162.          Left            =   600
  163.          TabIndex        =   2
  164.          Text            =   "2"
  165.          Top             =   255
  166.          Width           =   2610
  167.       End
  168.       Begin VB.Label Label2 
  169.          Caption         =   "To:"
  170.          Height          =   165
  171.          Left            =   3570
  172.          TabIndex        =   3
  173.          Top             =   285
  174.          Width           =   315
  175.       End
  176.       Begin VB.Label Label1 
  177.          Caption         =   "From:"
  178.          Height          =   180
  179.          Left            =   150
  180.          TabIndex        =   1
  181.          Top             =   300
  182.          Width           =   465
  183.       End
  184.    End
  185.    Begin VB.Label Label7 
  186.       Caption         =   $"Demo.frx":0000
  187.       BeginProperty Font 
  188.          Name            =   "MS Sans Serif"
  189.          Size            =   8.25
  190.          Charset         =   0
  191.          Weight          =   700
  192.          Underline       =   0   'False
  193.          Italic          =   0   'False
  194.          Strikethrough   =   0   'False
  195.       EndProperty
  196.       Height          =   945
  197.       Left            =   15
  198.       TabIndex        =   21
  199.       Top             =   5760
  200.       Width           =   7635
  201.    End
  202.    Begin VB.Label Label6 
  203.       Caption         =   "It seems many of you are having problems showing forms from Threads.... So I have added a ""Form Show"" Demonstration"
  204.       Height          =   375
  205.       Left            =   0
  206.       TabIndex        =   20
  207.       Top             =   4935
  208.       Width           =   7425
  209.    End
  210.    Begin VB.Label Label5 
  211.       Caption         =   $"Demo.frx":0128
  212.       Height          =   450
  213.       Left            =   15
  214.       TabIndex        =   16
  215.       Top             =   840
  216.       Width           =   7590
  217.    End
  218.    Begin VB.Label Label4 
  219.       Caption         =   "If you believe this article deserves it, please click the button alongside to get the voting dialog box "
  220.       BeginProperty Font 
  221.          Name            =   "MS Sans Serif"
  222.          Size            =   8.25
  223.          Charset         =   0
  224.          Weight          =   700
  225.          Underline       =   0   'False
  226.          Italic          =   0   'False
  227.          Strikethrough   =   0   'False
  228.       EndProperty
  229.       Height          =   585
  230.       Left            =   2145
  231.       TabIndex        =   15
  232.       Top             =   4365
  233.       Width           =   3240
  234.    End
  235. Attribute VB_Name = "Form1"
  236. Attribute VB_GlobalNameSpace = False
  237. Attribute VB_Creatable = False
  238. Attribute VB_PredeclaredId = True
  239. Attribute VB_Exposed = False
  240. 'NOTE:MThreadVB does not use the standard apartment model
  241. 'threading... As a result global and module level variables
  242. 'are now accessible to the thread and they do not remain
  243. 'hidden
  244. 'Though you can "officially" pass only a variant a parameter
  245. 'this really does not hold true in this case
  246. 'In this example for instance, the module level
  247. 'variables Min and Max are
  248. 'the actual parameters which are accessed by the thread
  249. 'procedure FindPrimes(). Since VB requires it, the
  250. 'multithreaded Sub must also have a single variant argument
  251. 'Here this argument is just a dummy though and the actual
  252. 'parameters are the variables Min and Max that are
  253. 'accessed by the Thread Procedure
  254. 'Also the variable Primes holds the prime numbers that
  255. 'are found (The output variable)
  256. Dim WithEvents FormThread As MThreadVB.Thread
  257. Attribute FormThread.VB_VarHelpID = -1
  258. Dim WithEvents pThread As MThreadVB.Thread
  259. Attribute pThread.VB_VarHelpID = -1
  260. Dim Min As Long, Max As Long
  261. Dim Primes As String
  262. Dim Frm As New VDialog
  263. Private Sub Picture1_Click()
  264. End Sub
  265. Private Sub Command1_Click()
  266. ELog.Text = ""
  267. End Sub
  268. Private Sub EndThread_Click()
  269.     pThread.TerminateWin32Thread
  270. End Sub
  271. Private Sub Form_Load()
  272. 'Instantiate a reference to the multithreader library
  273. Set pThread = New Thread
  274. Set FormThread = New Thread
  275. Pr.AddItem "Lowest"
  276. Pr.AddItem "Below Normal"
  277. Pr.AddItem "Normal"
  278. Pr.AddItem "Above Normal"
  279. Pr.AddItem "Highest"
  280. Pr.ListIndex = 2
  281. End Sub
  282. Sub FindPrimes(DummyArgument As Variant)
  283. 'This is the actual multithreaded sub.
  284. 'It is called by MthreadVB and made to run on a different
  285. 'thread
  286. 'All multithreaded subs must accept a variant as an argument
  287. 'In this case the argument is a dummy, and the actual
  288. 'arguments are Min and Max which are set when the
  289. 'Start Prime Thread is clicked
  290. 'The variable Primes contains the prime numbers
  291. 'Since it is a module level variable it can be accessed by
  292. 'any sub in this module
  293. Dim PCnt As Long, I As Long, J As Long
  294. If Max = 0 Then GoTo 20
  295. For I = Min To Max
  296.     If I = 0 Then GoTo 10
  297.     If I = 1 Then GoTo 10
  298.     For J = 2 To I - 1
  299.         If I Mod J = 0 Then
  300.             GoTo 10
  301.         End If
  302.     Next J
  303.     Primes = Primes + CStr(I) + Chr$(13) + Chr$(10)
  304. 10 Next I
  305. 20 End Sub
  306. Private Sub Form_Unload(Cancel As Integer)
  307.     If pThread.IsThreadRunning = True Or FormThread.IsThreadRunning = True Then
  308.         MsgBox "A thread is running. Terminate it before quitting"
  309.         Cancel = True
  310.     End If
  311.     Set pThread = Nothing
  312.     Set FormThread = Nothing
  313.     'VERY IMPORTANT - MUST EXPLICITLY CALL END TO TERMINATE APP
  314.     End
  315. End Sub
  316. Private Sub Pr_Click()
  317. If pThread.IsThreadRunning = True Then
  318. Select Case Pr.List(Pr.ListIndex)
  319.     Case "Lowest"
  320.         pThread.ThreadPriority = THREAD_PRIORITY_LOWEST
  321.     Case "Below Normal"
  322.         pThread.ThreadPriority = THREAD_PRIORITY_BELOW_NORMAL
  323.     Case "Normal"
  324.         pThread.ThreadPriority = THREAD_PRIORITY_NORMAL
  325.     Case "Above Normal"
  326.         pThread.ThreadPriority = THREAD_PRIORITY_ABOVE_NORMAL
  327.     Case "Highest"
  328.         pThread.ThreadPriority = THREAD_PRIORITY_HIGHEST
  329. End Select
  330. End If
  331. End Sub
  332. Private Sub pThread_OnThreadCreateFailure()
  333.     ELog.Text = ELog.Text & Chr$(13) & Chr$(10) & "Thread could not be Created"
  334. End Sub
  335.      
  336. Private Sub pThread_OnThreadCreateSuccess(ByVal ThreadHandle As Long, ByVal ThreadID As Long)
  337.     ELog.Text = ELog.Text & Chr$(13) & Chr$(10) & "Thread Created (Calculations started)"
  338.     StartThread.Enabled = False
  339.     EndThread.Enabled = True
  340. End Sub
  341. Private Sub pThread_OnThreadFinish(ByVal ThreadHandle As Long, ByVal ThreadID As Long)
  342.     ELog.Text = ELog.Text & Chr$(13) & Chr$(10) & "Thread has finished running(Calculations Ended)"
  343.     PText.Text = ""
  344.     PText.Text = Primes
  345.     Primes = ""
  346.     StartThread.Enabled = True
  347.     EndThread.Enabled = False
  348. End Sub
  349. Private Sub pThread_OnThreadPriorityChange(ByVal ThreadHandle As Long, ThreadID As Long, ByVal OldPriority As MThreadVB.ThreadPriorityConsts, ByVal NewPriority As MThreadVB.ThreadPriorityConsts)
  350.     ELog.Text = ELog.Text & Chr$(13) & Chr$(10) & "Thread priority set or changed"
  351. End Sub
  352. Private Sub pThread_OnThreadTerminate(ByVal ThreadHandle As Long, ByVal ThreadID As Long, ByVal ExitCode As Long)
  353.     ELog.Text = ELog.Text & Chr$(13) & Chr$(10) & "Thread has been forcefully terminated"
  354.     StartThread.Enabled = True
  355.     EndThread.Enabled = False
  356. End Sub
  357. Private Sub ShowForm_Click()
  358. ShowModalForm.Enabled = False
  359. ShowForm.Enabled = False
  360. If FormThread.IsThreadRunning = False Then
  361. 'If no thread is launched by the FormThread object,
  362. 'we launch a new Thread.... The multithreaded procedure
  363. 'is called ShowFormMT, and here we actually pass
  364. 'a parameter (0) that determines whether the Form
  365. 'is modal or not
  366.     FormThread.CreateWin32Thread Me, "ShowFormMT", 0
  367. End If
  368. End Sub
  369. Sub ShowFormMT(ModalFlag As Variant)
  370. 'This is the multithreaded procedure... We cannot
  371. 'directly load a form here, since doing so will cause
  372. 'VB to crash... We must call the Main Thread and make
  373. 'it perform the Form load operation...
  374. 'The ObjectInThreadContext property returns a reference
  375. 'to the original Form (Form1) running on the original
  376. 'Thread... We call the ShowFormNow() Sub to show the form
  377. 'But this Sub is called in context to the original Thread
  378. FormThread.ObjectInThreadContext.ShowFormNow (CLng(ModalFlag))
  379. 'REMEMBER:Here in this case, ModalFlag is a "real"
  380. 'parameter unlike in the FindPrimes() Sub
  381. End Sub
  382. Private Sub ShowModalForm_Click()
  383. If FormThread.IsThreadRunning = False Then
  384. 'If no thread is launched by the FormThread object,
  385. 'we launch a new Thread.... The multithreaded procedure
  386. 'is called ShowFormMT, and here we actually pass
  387. 'a parameter (1) that determines whether the Form
  388. 'is modal or not (It is modal in this case)
  389.     FormThread.CreateWin32Thread Me, "ShowFormMT", 1
  390. End If
  391. End Sub
  392. Sub ShowFormNow(ModalFlag As Long)
  393. 'This sub is called ultimately, and it ultimately loads
  394. 'the Form (ModalFlag is the parameter determining
  395. 'whether the form is modal or not)
  396. VDialog.Show ModalFlag
  397. End Sub
  398. Private Sub StartThread_Click()
  399. Min = CLng(Start.Text)
  400. Max = CLng(EndBox.Text)
  401. 'This statement creates a thread. The second parameter is
  402. 'the Function that has be multithreaded. The first parameter
  403. 'is the Object on which the Function is defined...
  404. pThread.CreateWin32Thread Me, "FindPrimes", 0
  405. PText.Text = ""
  406. Select Case Pr.List(Pr.ListIndex)
  407.     Case "Lowest"
  408.         pThread.ThreadPriority = THREAD_PRIORITY_LOWEST
  409.     Case "Below normal"
  410.         pThread.ThreadPriority = THREAD_PRIORITY_BELOW_NORMAL
  411.     Case "Normal"
  412.         pThread.ThreadPriority = THREAD_PRIORITY_NORMAL
  413.     Case "Above Normal"
  414.         pThread.ThreadPriority = THREAD_PRIORITY_ABOVE_NORMAL
  415.     Case "Highest"
  416.         pThread.ThreadPriority = THREAD_PRIORITY_HIGHEST
  417. End Select
  418. End Sub
  419. Private Sub VDlg_Click()
  420.     VDialog.Show 1
  421. End Sub
  422.