Multithread problems

I have a dialog based project which encodes file from type A to type B. The functions related to the

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!
[2538 byte] By [forester] at [2007-11-20 11:48:48]
# 1 Re: Multithread problems
This is the line that cause the dialog to be "white blank rectangle" :

::WaitForSingleObject(m_pThread->m_hThread, INFINITE);


The above line of code causes the main working thread to block until the worker thread completes.
Wombat at 2007-11-11 4:02:53 >
# 2 Re: Multithread problems
Wombat is exactly right. The UI thread is blocked by the call to WaitForSingleObject and the dialog doesn't repaint because it can't receive any repaint messages because the message pump is not running.

Check out the Simple Thread: Part I article link in my subject line. It shows how to start, pause, resume, and stop a thread. It shows how you can do this without blocking the UI thread. The article has source code that shows how to disable the buttons that control the thread controlling buttons and reenable them when the thread finishes.

If you have any suggestions to improve the article, please let me know.
Arjay at 2007-11-11 4:03:48 >
# 3 Re: Multithread problems
I have 2 questions: ...I guess you should have ask one more question: what to do to avoid that?
If you want your app to be responsive (and we all do) - do NOT Wait() or Sleep() on the main UI thread.
You should kick off a thread and return to your other tasks (process messages).
If you want / need to be notified when the thread is finished - the thread should post some message back to your window. That is up to that thread's programmer. I see that you pass your "this" pointer to the thread - how is it goig to use it? Is your CMyDialog derived from MFC's CDialog? Then it is NOT thread safe. You need to read documentation for that third party DLL to see what they expect and how they will signal back to you the completion of work.
VladimirF at 2007-11-11 4:04:47 >
# 4 Re: Multithread problems
Thank you all for you replies!
Yes, it is the WaitForSingleObject() that blocks the UI main thread, before I started this thread, I had tried to comment out this line, then there was no any white blank rectangle.

But I must wait for the completion of EncodeThread, then I can call any other functions, now the WaitForSingleObject() has side effect, what is the other proper methed? Thanks!

I have tried the Event synchronization, still the same problem.
forester at 2007-11-11 4:05:51 >
# 5 Re: Multithread problems
Check out the Simple Thread: Part I article link in my subject line. It shows how to start, pause, resume, and stop a thread. It shows how you can do this without blocking the UI thread. The article has source code that shows how to disable the buttons that control the thread controlling buttons and reenable them when the thread finishes.


Where can I get the "Check out the Simple Thread: Part I article link in my subject line. ...", Thanks!
forester at 2007-11-11 4:06:56 >
# 6 Re: Multithread problems
Arjay's signature has the links, but you may have your preferences set to NOT display signatures. Check your user CP, then view Arjay's post again.
JVene at 2007-11-11 4:07:58 >
# 7 Re: Multithread problems
Thanks JVene, I forgot that you can choose not to display the signature line.

Here's the article link:

Simple Thread: Part I ( http://www.dev-archive.com/cpp/misc/misc/threadsprocesses/article.php/c14105/)
Arjay at 2007-11-11 4:08:51 >
# 8 Re: Multithread problems
Thank you Arjay for you wonderful articles. They are just what I need.
forester at 2007-11-11 4:09:54 >