Solved

Modeless Dialog and Threads

Posted on 2001-09-02
9
345 Views
Last Modified: 2013-11-20
Hello again folks, Here's my situation:

I have an app that starts a worker thread that parses an XML File. As this worker thread is parsing, I want to update a modeless dialog which is displayed before the worker thread is started. This modeless dialog is sort of a progress dialog as I want it to display the progress of the parsing that the worker thread is performing.

Now my problem is that when the Modeless is displayed and the worker thread starts, I can't interact with the modeless until the worker thread is finished.

Any idea what could be happeing?
0
Comment
Question by:ruff_ryder
  • 4
  • 4
9 Comments
 
LVL 2

Accepted Solution

by:
xu2000 earned 150 total points
ID: 6449579
Hi,
1. Create your modeless dialog (m_pYourDlg is a member varible)
if(m_pYourDlg->GetSafeHwnd() == 0)
{
     if(m_pYourDlg->Create() == TRUE)
     {
          ...
          m_pYourDlg->ShowWindow(SW_SHOW);
     }
}

2. Start your workthread,
CWinThread* pThread = AfxBeginThread(WorkThreadProc, m_pYourDlg->GetSafeHwnd(),
                   THREAD_PRIORITY_NORMAL);
                   
3. In your workthread
UINT WorkThreadProc(LPVOID pParam)
{
     ...
     while(...)
     {
          parse XML file
          ...
          ::PostMessage((HWND)pParam, WM_STEPIT, 0, 0);
     }
     return 0;
}

4. Define a user message WM_STEPIT and add a function for it in YourDlg.cpp
LRESULT CYourDlg::OnMyMessage(WPARAM wParam, LPARAM lParam)
{
     ...
     step your progress control
     ...
}

Another way is define a global varible to interact with the modeless dialog

My 2 cents
Xu
0
 

Expert Comment

by:rsraj
ID: 6451013
Hi,

Instead of PostMessage, you may use SendMessage which is synchronous.

Also to ensure your modeless dialog box gets its due time,
you may use Event object.
After PostMessage you may use WaitForSingleObject(hEvent...) to ensure that the dialog box PostMessage function processesing is successful.

rgds
sun
0
 
LVL 2

Expert Comment

by:xu2000
ID: 6451035
Hi,
Progress control is only indicator of parsing file, important thing is parsing file, so I think PostMessage is the better way.

Xu
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 3

Author Comment

by:ruff_ryder
ID: 6451826
Hi guys, Im new to the whole Windows Programming (MFC Programming) so if you can, would you give a little more detail as to what the difference is between SendMEssage and PostMessage ?
0
 
LVL 2

Expert Comment

by:xu2000
ID: 6452039
The SendMessage function sends the specified message to a window or windows. It calls the window procedure for the specified window and does not return until the window procedure has processed the message.
The PostMessage return immediately.

MSDN has the detail. If encount problem about windows programming, consult MSDN first.

Xu
0
 
LVL 2

Expert Comment

by:xu2000
ID: 6456279
Hi ruff_ryder:
Do you have other questions?
Xu
0
 
LVL 3

Author Comment

by:ruff_ryder
ID: 6540181
Hi guys, sorry I've been so long with this but I'm just getting time to get back to it. I've tried using both PostMessage and SendMessage but neither is giving me the results I require ... the main window is still frozen while the worked thread is processing and i only get back control after its done ...
0
 
LVL 3

Author Comment

by:ruff_ryder
ID: 6622530
Hi guys, I found a workaround to the prob. Used a bit of callback to achieve the required result.

Thanks for your help though. Since xu2k had the msot input I'll give him the points.
0
 
LVL 3

Author Comment

by:ruff_ryder
ID: 6622538
Tried your code in a separate prog and it works :)
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

805 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question