To use thread in programming in vb6

删除回忆录丶 提交于 2019-11-26 08:39:03

问题


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


回答1:


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.




回答2:


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.




回答3:


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.




回答4:


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&postcount=4




回答5:


I believe the Matt Curland ActiveX exe method is the classic method. Alternatively you could use DoEvents. Or delegate the work to a separate ActiveX exe like this. It would be nice to you also use the .NET BackgroundWorker component but it turns out that doesn't work.



来源:https://stackoverflow.com/questions/383162/to-use-thread-in-programming-in-vb6

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!