-
September 9th, 2012, 11:57 AM
#1
[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.
-
September 9th, 2012, 01:40 PM
#2
Re: Please estimate this code?
And what problem do you have with it?
Victor Nijegorodov
-
September 9th, 2012, 01:50 PM
#3
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.
-
September 9th, 2012, 01:54 PM
#4
Re: Please estimate this code?
If you "afraid" then test it!
Why do we need to make this work for you?
Victor Nijegorodov
-
September 9th, 2012, 01:56 PM
#5
Re: Please estimate this code?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|