Saturday, March 1, 2014

How to Create a Thread From Inside a Dynamic Link Library


1. Open Microsoft Visual Basic Express, select the 'File' menu and click 'New Project.' Select 'Visual Basic' under 'Installed Templates' then double-click 'Class Library' shown in the middle of the 'New Project' Window.
2. Type the following above 'Public Class Class1':Imports SystemImports System.Threading
3. Copy and paste the following under 'Public Class Class1':Private Sub CntrFunc()Dim iCntr As IntegerDim threadState As StringFor iCntr = 1 To 70000If iCntr Mod 7000 = 0 ThenthreadState = Thread.CurrentThread.ThreadState.ToStringConsole.WriteLine('Counter: ' + threadState)End IfNextEnd SubThis subroutine will be started as a thread and execute a For loop.
4. Type the following to create a the function that will start the 'CntrFunc' thread and monitor its status until it's done:Public Function createThreadInDLL() As StringDim threadstate As StringDim tr As New Thread(AddressOf CntrFunc)tr.Start()While tr.IsAliveThread.CurrentThread.Sleep(200)End Whilethreadstate = tr.ThreadState.ToStringcreateThreadInDLL = 'Thread is done! Thread is: ' + _threadstateEnd Function

No comments:

Post a Comment