CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Hybrid View

  1. #1
    Join Date
    Feb 2012
    Posts
    181

    [RESOLVED] Please estimate this code?

    This main part of my program. Please estimate this code. What problems can be?
    Code:
    void Writer(void *args)
    {
      Writing = TRUE;
      Stop    = FALSE;
      HANDLE hf;
     if (InitPort())
      {
    	MessageBox(hwnd,L"Error open file", L"Error", 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"Error opening file", L"Error", 0);         
    	Writing = FALSE;
        return;	 
      }
      DWORD sz = GetFileSize(hf, NULL); 
      unsigned DivisionCount = 20;
      if (sz <= 256) DivisionCount = 1;
             else
        {if (sz <= 256*20) DivisionCount = sz/256;} 
      SendMessage(hProgress, PBM_SETRANGE, 0, MAKELPARAM(0, DivisionCount));
      SendMessage(hProgress, PBM_SETPOS, 0, 0);
      unsigned CycleCount = sz/(256*DivisionCount);
    
      DWORD tmpBytesRead;
      DWORD Written;
      int szName = strlen(TempName);
      if (!WriteFile(hComPort, TempName, szName, &Written, NULL))
          {	     
     	    MessageBox(hwnd,L"Error send to port", L"Error", 0);         
     	    Writing = FALSE;
            return;	 
          }
      if (!WriteFile(hComPort, &STX, 1, &Written, NULL))
    	  {
    	    MessageBox(hwnd,L"Error send to port", L"Error", 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;
      SendMessage(hProgress, PBM_SETPOS, 0, 0);
      while (Index < sz)    
      {
        int Division = CycleIndex/5;
        if (Division*5 == CycleIndex) 
    	{
         WCHAR Temp[100]; Temp[0] = 0;
    	 wcscat(Temp, L"Осталось: ");
    	 int Seconds = 0.015*(sz-Index)/256 + ((sz-Index)*8)/ComRate;
         WCHAR Sec[16]; Sec[0] = 0;
         swprintf(Sec, L"%u", Seconds);
    	 wcscat(Temp, Sec);
    	 wcscat(Temp, L" с");
         SetWindowText(hStatic, (LPCWSTR) Temp);
    	}
    	CycleIndex++;
    	if (CycleIndex == CycleCount)
    	{
    	  CycleIndex = 0;
    	  ProgressPos++;
          SendMessage(hProgress, PBM_SETPOS, ProgressPos, 0);
    	}
        if (Stop)
    	{
    	  WriteFile(hComPort, &Eof, 1, &Written, NULL);
    	  Writing = FALSE;
    	  CloseHandle(hf);
    	  CloseHandle(hComPort);
          SendMessage(hProgress, PBM_SETPOS, 0, 0);
          SetWindowText(hStatic, (LPCWSTR) L"");
    	  return;	   
    	}
        if ((sz-Index) < 256) ReadSize = sz-Index;
        if (!ReadFile(hf, buff, ReadSize, &tmpBytesRead, NULL))
    	{
    	  MessageBox(hwnd,L"Error read file", L"Error", 0);         
    	  WriteFile(hComPort, &Eof, 1, &Written, NULL);
    	  Writing = FALSE;
    	  CloseHandle(hf);
    	  CloseHandle(hComPort);
          SendMessage(hProgress, PBM_SETPOS, 0, 0);
          SetWindowText(hStatic, (LPCWSTR) L"");
    	  return;
    	}
        if (Stop) 
    	{
    	  WriteFile(hComPort, &Eof, 1, &Written, NULL);
    	  Writing = FALSE;
    	  CloseHandle(hf);
    	  CloseHandle(hComPort);
          SendMessage(hProgress, PBM_SETPOS, 0, 0);
          SetWindowText(hStatic, (LPCWSTR) L"");
    	  return;	   
    	}
        if (!WriteFile(hComPort, buff, ReadSize, &Written, NULL))
    	{
    	  MessageBox(hwnd,L"Error read file", L"Error", 0);         
    	  WriteFile(hComPort, &Eof, 1, &Written, NULL);
    	  Writing = FALSE;
    	  CloseHandle(hf);
    	  CloseHandle(hComPort);
          SendMessage(hProgress, PBM_SETPOS, 0, 0);
          SetWindowText(hStatic, (LPCWSTR) L"");
    	  return;
    	}
        Sleep(15);
    	Index = Index + ReadSize;
      }
      WriteFile(hComPort, &Eof, 1, &Written, NULL);
      Writing = FALSE;
      CloseHandle(hf);
      CloseHandle(hComPort);
      SendMessage(hProgress, PBM_SETPOS, 0, 0);
      SetWindowText(hStatic, (LPCWSTR) L"");
      MessageBox(hwnd,L"Ready", L"Complete", 0);
      return;
    }
    Last edited by AKE; September 9th, 2012 at 12:01 PM.

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Please estimate this code?

    And what problem do you have with it?
    Victor Nijegorodov

  3. #3
    Join Date
    Feb 2012
    Posts
    181

    Re: Please estimate this code?

    VictorN
    Nothing. But I afraid to that this code can consist "non-clear" incorrect places, errors. Now I don't detect their. Procedure is invoked as thread.

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Please estimate this code?

    If you "afraid" then test it!
    Why do we need to make this work for you?
    Victor Nijegorodov

  5. #5
    Join Date
    Feb 2012
    Posts
    181

    Re: Please estimate this code?

    Thanks

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured