Solved

Modeless Dialog and Threads

Posted on 2001-09-02
9
339 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 2

Expert Comment

by:xu2000
Comment Utility
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
Comment Utility
Hi ruff_ryder:
Do you have other questions?
Xu
0
 
LVL 3

Author Comment

by:ruff_ryder
Comment Utility
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
Comment Utility
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
Comment Utility
Tried your code in a separate prog and it works :)
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
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 remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

771 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

10 Experts available now in Live!

Get 1:1 Help Now