Solved

Modeless Dialog and Threads

Posted on 2001-09-02
9
353 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

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: 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.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

740 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