Multithread problems
encoding are export from a third party DLL. When the user clicks the button of "Encode file", a thread
named EncodeThread will be created, and all the encoding works are done in the EncodeThread.
Normally, it will take 1 or 2 minutes for a file of type A with size of 5M to be encoded into file type
B.
void CMyDialog::OnButtonEncodeFile()
{
// dialog didn't re-paint when thread is running
m_pThread=::AfxBeginThread(EncodeThread, (LPVOID)this);
::WaitForSingleObject(m_pThread->m_hThread, INFINITE);
...
}
When I ran the EXE, I clicked the "Encode file" button, the EncodeThread thread began to run, during
the waiting, I swithed to other windows, then swtiched back, then the dialog always appeared as a white
blank rectangle, it would restore only after the completion of the EncodeThread. But the dialog could
move around, just didn't re-paint the user interface.
There are 2 main types of functions in the EncodeThread , one are functions exported from the third
party DLL which encode data in the buffer, the other are the C functions, fopen(), fread(), fwrite()
etc, the fread() reads data from file A into data buffer in a while() loop, the dll functions then
encode the data buffer.
I have 2 questions:
Q1, Did the fread, fwrite or some encoding functions which are time consuming in the thread of
EncodeThread cause the dialog to be shown as white blank rectangle? if yes, how to resolve? if not what else caused the problem?
Q2, where should I call the hInstance=::LoadLibrary("some.dll"), should it be in the dialog's
OnInitDialog() or put it in the thread funcion of EncodeThread?
i.e.
BOOL CMyDialog::OnInitDialog()
{
m_hInstance=::LoadLibrary("some.dll");
...
...
}
OR
UINT EncodeThread(LPVOID lpParam)
{
hInstance=::LoadLibrary("some.dll")
ENCODEFUN1 * Encodefun1=::GetProcAddress(hInstance,"ENCODEFUN1");
ENCODEFUN2 * Encodefun2=::GetProcAddress(hInstance,"ENCODEFUN2");
...
while(...)
{
...
fread();
Encodefun1(buffer,...);
Encodefun2(buffer,...);
fwrite();
...
}
...
//end of encoding
::FreeLibrary(hInstance);
}
Any help will be appreciated!

