views:

939

answers:

5

I am programming to generate keys in hexadecimal using different random function and write it in the text file. I have only two cmd buttons and status bar which displays the current time and the status of the process. Now on clicking the cmd button for keygeneration the form gets locked up and all the other activity is suspended that is even the time is suspended until the process is over. i can even not abort the process in between. I want to solve this problem using the thread if possible. how do i do that please suggest. Else if there is any other method to sort out this issue please suggest.

Thankyou in anticipation of the valuable help

+2  A: 

You can use the CreateThread Win32 API but please keep in mind that the VB6 debugger cannot handle multiple threads.

Also, if you have loops of any kind, try slapping "DoEvents" somewhere in the loop. It will severely cut back on performance but your forms will be responsive.

dreamlax
A: 

It might not be possible, but you might want to consider switching to .NET, where VB has full support for threading.

Another cheeky approach (not very good, but it works) might to shell an exe to do the work, and have your VB6 simply poll for a results file. This will be a separate process, so completly isolated thread(s). Really messy, and I don't recommend it.

Marc Gravell
Another cheeky approach, but much less messy, is to write an ActiveX exe to do the work. Automatically a separate process and completely isolated threads, but you can pass data between the threads and let COM do the marshalling for you. Signal completion of the task through a callback to the original client. It's written up in Dan Appleman's book "developing com/ActiveX components with VB6"
MarkJ
... and I've written more about it in this answer. http://stackoverflow.com/questions/727386/making-a-c-kill-event-for-a-vb6-app/752841#752841
MarkJ
+6  A: 

The only "legal" way to do multi-threading in VB6 is through ActiveX EXEs -- just use the thread per object option on the project properties dialog. Matt Curland has a good example how to convert your Standard EXE to a multi-threaded ActiveX EXE. Doing it this way allows VB6 debugger to works w/o crashes because in the IDE everything is executed on a single thread.

If you want to cut down the overhead of ActiveX EXE multi-threading then you have to use in-proc multi-threading which is not supported but still doable. Check Compact In-Process Multi-threading: A FolderWatcher with sample UI for a way to safely use CreateThread and to safely initialize VB6 run-time on the new thread (courtesy Matt Curland again).

You might want to check his Advanced Visual Basic 6 book for more details.

cheers,
</wqw>

wqw
Check the Markdown reference about how to do hyperlinking properly. You can't use Standard HTML.
ConcernedOfTunbridgeWells
one funny thing that i've noticed when i use the activex exe for multithreading is that the ide debug mode doesn't behave the same as when you build the exe and just run the exe. why is this?
melaos
@melaos: VB6 IDE is always single-threaded so thread per object ActiveX EXEs behave differently just to be debuggable.
wqw
A: 

You can also start a child process via CreateProcess and redirect the StdIO streams for IPC. Anonymous Pipe I/O is not nearly the hack that trying to communicate via disk files would be.

There are a few free components around for doing this.

http://www.xtremevbtalk.com/showpost.php?p=1304628&amp;postcount=4

Bob
A: 

I believe the Matt Curland ActiveX exe method is the classic method. Apparently you can also use the .NET BackgroundWorker component. Alternatively you could use DoEvents. Or delegate the work to a separate ActiveX exe like this.

MarkJ