Hello
When I create a thread in my vb6 program, I met some problems:
1 if i create a thread , in the debug mode , I can not terminate the thread.
I use createThread() function to create a thread as following:
rc = TerminateThread(gThreadHandle, exitCode)
CloseHandle (gThreadHandle)
2. if I make a exe. but the exe can not create the thread. and the message told me that some address can not be written.
I have no idea of solving these problems. Hope you can give me some tips.
thanks in advance.
the attachment is my project
VB is not able to do Multithreading in this way. There are some 'ways around that problem, like activeX controls can ave there own threads, or simulating threads, by some timerconstructions which in graphical programs can look like multitasking, even they arn't.
The solution is VC++ or C# or if VB needed then VB.net
Maybe you can use the free MS 2005 Compiler and you can try to transfer your code to VB.net.
Jonny Poet
To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
If anyone felt he has got help, show it in rating the post.
Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ? My latest articles : Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7
We have production VB6 code running multiple threads with no problems. It's not pretty, and Johnny's suggestion of porting to VB 2005 would simplify your life immensely, at least in terms of multithreading, though you'd likely find all kinds of other "issues" to deal with in the conversion, if you're way into your project.
However, if that's not a practical solution for you, I'd suggest the following:
Communicate with your "worker" thread and instruct it to terminate, instead of calling TerminateThread. That's not so clean, and as you've found, sometimes doesn't terminate the thread. There are a bunch of ways you could communicate with the other thread, one of which being a global "shut down" variable, that you could protect with a mutex, and change to true when you want the other thread to terminate. The other thread (presumably, it's in a loop of some sort, otherwise it would end on its own) would check this variable periodically (once per loop, perhaps), and exit the loop and terminate gracefully when it's time. Something along these lines will probably be your best bet.
Alternately, maybe your problem is that you're calling CloseHandle before the thread has a chance to terminate. Try calling Sleep after the TerminateThread call before you CloseHandle - dunno... I don't use TerminateThread unless I have to.
In your "worker thread" loop, you might also try calling Sleep(0) and see if that helps.
For what it's worth, beware of compiling Native Code applications that are multi-threaded, it likely won't work. I used to know why, but I haven't had to deal with it in a while. Since you can create the thread in the IDE (which always runs as P-Code), and not in the executable, I'd guess that you're compiling to Native Code instead of P-Code, and that's your problem. If not, then what's your declaration for CreateThread look like? I'm particularly interested in the AddressOf parameter.
In any case, you'd probably do well to go to http://msdn.microsoft.com, and search for a thread function, such as CreateThread, and take the link at the bottom to the Threading Overview, and read the whole thing. There are a lot of concerns and pitfalls to creating, managing, synchronizing, and communicating with other threads... as a rule, you're going to want to be very familiar with threading concepts before you start churning out multi-threaded code, in any language.
---------------Semi-related---------------
For what it's worth, as for the access violation error, if you have Visual Studio installed, and you compiled the VB executable with the Create Symbolic Debug Info option checked, you should be able to click Cancel on the error dialog, start a debugging session in the VS debugger, and view the call stack, and back out to the VB Code that is causing the access violation (the call stack, itself, will likely be in kernel32.dll, user32.dll, or some other dll, which you'll probably see as a disassembly).
There are also some things you can do to identify the code without debugging it with the VS debugger, but it's really complicated and involved, and involves interrupting the VB compile process and turning on the switch to generate an assembly listing with the object modules. Then there's a bunch of math, etc, etc. It's not trivial. If you're interested, I'll dig out a document I wrote for our in-house developers, make it more general, and send it along.
Joe Plocki
Sometimes the straightest path is through the mud.
O|||||O
It's real easy to do using ActiveX.exe. If you take that approach, make sure your communication is as optimized as you can get it. In other words, try to to pass a buch of data back and forth.
Actually it's not VB's inability, VB just dont have local VB functions on threads, but VB's ability to let programmer use Win32 API completly balances it out.
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.