سلام به اقای موسوی و دیگر دوستان
موفق شدم با تعریف Thread مشکلم رو حل کنم ........
الان هم زمان متد رو برای هر تعداد کارت که بخوام صدا میزنم ....
برای دوستانی که آموزش Thread رو در C++ میخوان پیشنهاد می کنم این لینک رو مطالعه کنند:
http://www.codeproject.com/KB/threads/Threads_1.aspx
کدی که من نوشتم اینه :
char CurrentPath[_MAX_PATH];
GetCurrentPath(CurrentPath);
// read input from script file
FILE *fd = NULL;
CString ScriptFile;
ScriptFile.Append(CurrentPath);
ScriptFile.Append("\\install.txt");
fd = fopen (ScriptFile, "r+");
FILE *fd2 = NULL;
CString ScriptFile2;
ScriptFile2.Append(CurrentPath);
ScriptFile2.Append("\\install2.txt");
fd2 = fopen (ScriptFile2, "r+");
// error
if (fd == NULL)
{
::MessageBox( NULL, (LPCTSTR)lpMsgBuf, "Info", MB_OK | MB_ICONINFORMATION );
LocalFree( lpMsgBuf );
LogTxt("ERROR: Could not open scriptfile.");
ret = -1;
goto end;
}
// error
if (fd2 == NULL)
{
::MessageBox( NULL, (LPCTSTR)lpMsgBuf, "Info", MB_OK | MB_ICONINFORMATION );
LocalFree( lpMsgBuf );
LogTxt("ERROR: Could not open scriptfile.");
ret = -1;
goto end;
}
//Load Dll
CString PathDll;
PathDll.Append(CurrentPath);
PathDll.Append("\\GDll.dll");
HINSTANCE gpsh = LoadLibrary(PathDll);
if(gpsh == NULL)
{
::MessageBox( NULL, (LPCTSTR)lpMsgBuf, "Info", MB_OK | MB_ICONINFORMATION );
LocalFree( lpMsgBuf );
LogTxt("ERROR: Unable to load library! (gpsh == NULL)");
ret = -1;
goto end;
}
//Load function
handleCommands=(cfunc)GetProcAddress((HMODULE)gpsh , "handleCommands");
if((handleCommands==NULL))
{
::MessageBox( NULL, (LPCTSTR)lpMsgBuf, "Info", MB_OK | MB_ICONINFORMATION );
LocalFree( lpMsgBuf );
LogTxt("Unable to load function.");
FreeLibrary((HMODULE)gpsh);
ret = -1;
goto end;
}
else
{
//Create Handle of Thread
HANDLE Handle_Of_Thread_1 = 0;
HANDLE Handle_Of_Thread_2 = 0;
// Aray to store thread handles
HANDLE Array_Of_Thread_Handles[3];
//convert int to string [buf] and show reader ID
int readerid= d->readerID();
char buf[5];
itoa(readerid, buf, 10);
//MessageBox( NULL, buf, "reader id", MB_OK | MB_ICONINFORMATION );
//Call the Function
if(readerid == 2)
{
Handle_Of_Thread_1 = CreateThread( NULL, 0, (LPTHREAD_START_ROUTINE)handleCommands, fd, 0, NULL);
//ret = handleCommands(fd2,readerid);
}
else
{
Handle_Of_Thread_2 = CreateThread( NULL, 0, (LPTHREAD_START_ROUTINE)handleCommands, fd2, 0, NULL);
//ret = handleCommands(fd,readerid);
}
Array_Of_Thread_Handles[0] = Handle_Of_Thread_1;
Array_Of_Thread_Handles[1] = Handle_Of_Thread_2;
// Wait until all threads have terminated.
WaitForMultipleObjects(2,Array_Of_Thread_Handles, TRUE, INFINITE);
// Close all thread handles upon completion.
CloseHandle(Handle_Of_Thread_1);
CloseHandle(Handle_Of_Thread_2);
در کد بالا ابتدا dll رو لود کردم و متد مورد نظر رو معرفی کردم ... سپس به قسمت تردها میرسیم که با کامنتهایی توضیح دادم.