ATTENTION: You are viewing a page formatted for mobile devices; to view the full web page, click HERE.

Other Software > Developer's Corner

Mutex Problem

<< < (2/3) > >>

f0dder:
And in case Jibz should ever change his signature, I'll post the second line from it here, because it's scaringly true, and somewhat related to this thread (a mutex can be used as a threading synchronization primitive, after all):
"Multithreading is just one damn thing after, before, or simultaneous with another" -Andrei Alexandrescu
--- End quote ---

Deozaan:
And I'll quote the first line, because a lot of the times when I'm "stuck" on a programming issue I can't figure out, I end up thinking of the solution in the process of describing the problem to someone I'm asking for help.

"A problem, properly stated, is a problem on it's way to being solved" -Buckminster Fuller
--- End quote ---

Codebyte:
How do I free these Threads on Terminate?


--- ---//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
HANDLE Thread;
//---------------------------------------------------------------------------
DWORD WINAPI Thread1(LPVOID Param);
DWORD WINAPI Thread2(LPVOID Param);
//---------------------------------------------------------------------------
DWORD WINAPI Thread2(LPVOID Param)
{
//code here
ShowMessage("test2");
//terminate Thread
TerminateThread(Thread, false);
return 0;
}
//---------------------------------------------------------------------------
DWORD WINAPI Thread1(LPVOID Param)
{
//code here
ShowMessage("test");
//terminatethread
TerminateThread(Thread, false);
return 0;
}
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
//initialize the thread to CheckRegistry
DWORD Id;
Thread = CreateThread(0, 0, CheckRegistry, Form1->Handle, CREATE_SUSPENDED, &Id);
ResumeThread(Thread);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
//initialize the thread to CheckRegistry
DWORD Id;
Thread = CreateThread(0, 0, RefreshStore, Form1->Handle, CREATE_SUSPENDED, &Id);
ResumeThread(Thread);
}
//---------------------------------------------------------------------------

Jibz:
If you do not need access to the thread while it is doing it's job, you can free the handle you get from CreateThread right away with CloseHandle.

If you do need access to it, it probably means you are going to be waiting for some kind of result from it, which you can do using the WaitForSingleObject/WaitForMultipleObjects functions.

By the way, it appears there is a potential problem in your code; if the user presses Button1 a thread is created and the handle is stored in a global variable, if the user presses Button2 while the thread is running, this global variable is overwritten with the handle of the second thread.

Codebyte:
Here is my problem... no matter what I do I seem to be getting these errors that cause my program to bug out on close... I get Error 1400: Invalid Window Handle... but I have been able to identify that this occurs only when some threads are created... I cant seem to find the problem so I think that there is something I'm missing when it comes to this whole thread thing... Basically, the extent of my program is using a thread to take care of ftp work in the background so the ftp component wont lock up the program... it seems to work great except that I tend to get bugs here and there... idk though... I got Error 1400: Invalid Window Handle on RAD Studio startup screen earlier tonight.. 1st time ever... so it might not be me... any ideas? i can send you the source in private if u need... i've just smacked into the wall and cant get passed it

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version