c - CreateFile / readfile & writefile multi threading - invalid handle -
Do not bother me why you do not understand it:
void __cdecl Main_pipe_client (zeroes * args) {Py_func python_func; BOOL bRet; DWORD size = 0; Hpipe = CreateFile (TEXT ( "D: / stc_5010"), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_HIDDEN, NULL); If (hpipe == INVALID_HANDLE_VALUE) {// its ok, there is no error} while (1) {cell vidmos [50]; BRET = ReadFile (HPEEP, ViDOSOSAC, 50, End Size, Nouvel); // Readings Online (BRT == False) {python_func.py_AppendChat (1, "Reading failed: Bytes read:% U", size); break; } If (size> gt; 0) {// is OK} Sleep (1500); Size = 0; }}
hpipe - has been announced at the beginning of the program Handle hippie;
zero init () {sleep (500); Py_func python_func; Four buff [256]; DWORD wpisano = 0; Sprintf_s (buf, "INIT_CHARACTER"); If (lipfile (hipip, buff, 256, and wipesano, tap)) (four buff 5 [256]; Format Message (FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError (), MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT), ice 5, 256, NULL); Sprintf_s (buf5, "% s% d", buf5, GetLastError ()); Message Box A (NULL, (LPCRR) Buff 5, (LPCRR) "Error", Faucet); // return error 6}}
But if I am creating a loop, main_pipe_client function is fine in writefile. And the data is written in the file. But if I use this after func init ()
handles hThread = (handle) _beginthread (main_pipe_client, 0, NULL);
Then write file invalid handle ...
Since the pipes are made inside a thread, the hpipe handle may not be valid when WriteFile () is implemented. You need to 'synchronize' the thread to work properly.
code a "MyPipeCreateEvent that create a" <> main_pipe_client () will be set after a successful to CreateFile ()
. Go to WriteFile only when your event is set on "MyPipeCreateEvent" in your init ()
, WaitForSingleobject ()
Comments
Post a Comment