Solved

Modeless Dialog and Threads

Posted on 2001-09-02
9
342 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
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 tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

863 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now