[RESOLVED] One thread stop all system!! How avoid it? :(
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: [RESOLVED] One thread stop all system!! How avoid it? :(

  1. #1
    Join Date
    Feb 2012
    Posts
    181

    [RESOLVED] One thread stop all system!! How avoid it? :(

    Windows 7. Only mouse work and the thread.
    What incorectly??
    Code:
     WriterThread = (HANDLE) _beginthread(Writer, 0, NULL);
    Code:
    void Writer(void *args)
    {
      Writing = TRUE;
      Stop    = FALSE;
      HANDLE hf;
      HWND hProgress = CreateWindowEx(0, PROGRESS_CLASS, NULL, 
    		               WS_CHILD | WS_VISIBLE,
    			      20, 20, 260, 17,
    			      hwnd, NULL, hInst, NULL);
     
      if (InitPort())
      {
    	MessageBox(hwnd,L"asdfasdf", L"asdf", 0);         
    	Writing = FALSE;
        return;    
      }
      hf = CreateFile(FileName,          
                        GENERIC_READ,
                        0,
                        (LPSECURITY_ATTRIBUTES) NULL,
                        OPEN_EXISTING,
                        FILE_ATTRIBUTE_NORMAL,
                        (HANDLE) NULL);
      if (hf == INVALID_HANDLE_VALUE)
      {	     
    	MessageBox(hwnd,L"asdf", L"asdf", 0);         
    	Writing = FALSE;
        return;	 
      }
      DWORD sz     = GetFileSize(hf, NULL); // asdfasdf
      unsigned CycleCount = sz/(256*20);
      SendMessage(hProgress, PBM_SETRANGE, 0, MAKELPARAM(0, 20));
      DWORD tmpBytesRead;
      DWORD Written;
      int szName = strlen(TempName);
      if (!WriteFile(hComPort, TempName, szName, &Written, NULL))
          {	     
     	    MessageBox(hwnd,L"asdf", L"Ошибка", 0);         
     	    Writing = FALSE;
            return;	 
          }
      if (!WriteFile(hComPort, &STX, 1, &Written, NULL))
    	  {
    	    MessageBox(hwnd,L"asdf", L"asdf", 0);         
    		WriteFile(hComPort, &Eof, 1, &Written, NULL);
    	    Writing = FALSE;
            return;
    	  }
      int ProgressPos = 0; 
      unsigned Index = 0;
      int CycleIndex = 0;
      char buff[256];
      int ReadSize = 256;
      while (Index < sz)    
      {
        SendMessage(hProgress, PBM_SETPOS, ProgressPos, 0);
    	CycleIndex++;
    	if (CycleIndex == CycleCount)
    	{
    	  CycleIndex = 0;
    	  ProgressPos++;
    	}
        if (Stop)
    	{
    	  WriteFile(hComPort, &Eof, 1, &Written, NULL);
    	  Writing = FALSE;
    	  CloseHandle(hf);
    	  CloseHandle(hComPort);
    	  return;	   
    	}
        if ((sz-Index) < 256) ReadSize = sz-Index;
        if (!ReadFile(hf, buff, ReadSize, &tmpBytesRead, NULL))
    	{
    	  MessageBox(hwnd,L"asdf", L"asdf", 0);         
    	  WriteFile(hComPort, &Eof, 1, &Written, NULL);
    	  Writing = FALSE;
    	  CloseHandle(hf);
    	  CloseHandle(hComPort);
    	  return;
    	}
        if (Stop) // asdf
    	{
    	  WriteFile(hComPort, &Eof, 1, &Written, NULL);
    	  Writing = FALSE;
    	  CloseHandle(hf);
    	  CloseHandle(hComPort);
    	  return;	   
    	}
        if (!WriteFile(hComPort, buff, ReadSize, &Written, NULL))
    	{
    	  MessageBox(hwnd,L"asdf", L"asdf", 0);         
    	  WriteFile(hComPort, &Eof, 1, &Written, NULL);
    	  Writing = FALSE;
    	  CloseHandle(hf);
    	  CloseHandle(hComPort);
    	  return;
    	}
    	Index = Index + ReadSize;
      }
      WriteFile(hComPort, &Eof, 1, &Written, NULL);
      Writing = FALSE;
      CloseHandle(hf);
      CloseHandle(hComPort);
      MessageBox(hwnd,L"asdf", L"asdf", 0);
      return;
    }
    Last edited by AKE; August 30th, 2012 at 07:59 PM.

  2. #2
    Join Date
    Feb 2012
    Posts
    181

    Re: One thread stop all system!! How avoid it? :(

    Strange. It was only one time. But main window isn't active. This is trouble.

  3. #3
    Join Date
    Feb 2012
    Posts
    181

    Re: One thread stop all system!! How avoid it? :(

    I added Sleep. Now it such as work.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center