Need Help with Busy Dialog Thread

my application would perform a task. I want to stop the user from using the 
application's GUI untill the the BG task is finished.

At first using Modal Dlg did not do the trick as every one now (apart from 
me) the the CDialog::DoModal() function has to return some how.
So,
I made a bit of research on the net and found a trick to display the Dialog 
using CWinThread derived class without stoping the main app window for 
executing the background task as follows:
(1) In your header file define a CWinThread-derived class... 
class CDialogThread : public CWinThread
{
 DECLARE_DYNCREATE(CDialogThread)
 CDialogThread() {};
 virtual BOOL InitInstance();
public:
CBusyDlg* m_pBusyDlg;
BOOL KilDlg();
};

(2) in the implementation file 
IMPLEMENT_DYNCREATE(CDialogThread, CWinThread)
BOOL CDialogThread::InitInstance()
{
 m_pBusyDlg = new CBusyDlg();
 m_pBusyDlg->DoModal();
 return FALSE;
}
BOOL CDialogThread::KillDlg()
{
	if (m_pBusyDlg)
	{
		m_pBusyDlg->DestroyWindow();
		m_pBusyDlg = NULL;
		return TRUE;
	}
	return FALSE;
}
(3) Now in my main app declaration I have the following Var:
class CMyDoc : public CDocument
{
.....
....
CDialogThread* m_pDlgThread;
....
};
and in the implemtation file before my task starts I do the following:
CMyDoc::RunTask()
{
...........
.....
//m_pDlgThread = new CDialogThread();
m_pDlgThread = AfxBeginThread ( RUNTIME_CLASS(CDialogThread) );
.....
}
and when the task is finished, I want to destroy the Busydlg and kill its 
thread like so,
CMyDoc::FinishTask()
{
if (TRUE == CTaskManager::FinishTasks())
{	// returning false indicates that the op has finished
if (m_pDlgThread)
{
m_pDlgThread->KillDlg();
int ret = ::TerminateThread(m_pDlgThread->m_hThread, 0);
m_pDlgThread = NULL;
}
}
}


what happens is that the BusyDlg seems to vanish but I totally lose the 
focus to the main Window; can see it but can interact with GUI at all

Please, Can someone help with this approach or advise on another way

TIA
Viper
0
BrownViper (25)
5/17/2005 5:13:25 PM
vc.mfc 33608 articles. 0 followers. Follow

3 Replies
778 Views

Similar Articles

[PageSpeed] 32

BrownViper wrote:
> my application would perform a task. I want to stop the user from using the 
> application's GUI untill the the BG task is finished.
> 
> At first using Modal Dlg did not do the trick as every one now (apart from 
> me) the the CDialog::DoModal() function has to return some how.
> So,
> I made a bit of research on the net and found a trick to display the Dialog 
> using CWinThread derived class without stoping the main app window for 
> executing the background task as follows:
<code snipped>

You approach is awful.  Each window (such as your dialog) belongs to the 
thread that created it.  Calling a window from more than one thread 
leads to problems, possibly including deadlock.  Calling TerminateThread 
is also a poor design approach because it does not release resources 
allocated within the thread.

To stop the user from using the GUI until a task is finished you can 
simply disable the controls.  So you may not need a second thread at 
all.  But if you want the GUI to continue to do painting updates then 
the best approach is to put all GUI in the GUI thread (and disable 
controls when you want them disabled), and put the background operation 
in a secondary worker thread.  When the background operation completes 
it can post a message to the GUI thread, telling it to enable controls. 
  The background thread function then simply returns to cleanly close 
the thread.  This may be helpful:

http://www.mvps.org/vcfaq/mfc/index.htm

-- 
Scott McPhillips [VC++ MVP]

0
Scott
5/17/2005 9:29:12 PM
I m aware that my approach is awful, but what forced me to do it is the way 
my application is designed:
I have a Framewnd with 2 views on it, I use one for a visula feedback and 
the other carrying a set of GUI controls i.e. buttons, textboxes, combo,...etc
on to of that there are more GUI controls handled but the CDocument derived 
object. Not forgetting the App menu and the min,max,close buttons.
so to have a function to keep track of all these seems to me painfull and 
most MFC gurus would pull out thier revolvers.
that is why I used the CDialogThread approach.

Any comments on that would be appreciated

TIA 
Viper

"Scott McPhillips [MVP]" wrote:

> BrownViper wrote:
> > my application would perform a task. I want to stop the user from using the 
> > application's GUI untill the the BG task is finished.
> > 
> > At first using Modal Dlg did not do the trick as every one now (apart from 
> > me) the the CDialog::DoModal() function has to return some how.
> > So,
> > I made a bit of research on the net and found a trick to display the Dialog 
> > using CWinThread derived class without stoping the main app window for 
> > executing the background task as follows:
> <code snipped>
> 
> You approach is awful.  Each window (such as your dialog) belongs to the 
> thread that created it.  Calling a window from more than one thread 
> leads to problems, possibly including deadlock.  Calling TerminateThread 
> is also a poor design approach because it does not release resources 
> allocated within the thread.
> 
> To stop the user from using the GUI until a task is finished you can 
> simply disable the controls.  So you may not need a second thread at 
> all.  But if you want the GUI to continue to do painting updates then 
> the best approach is to put all GUI in the GUI thread (and disable 
> controls when you want them disabled), and put the background operation 
> in a secondary worker thread.  When the background operation completes 
> it can post a message to the GUI thread, telling it to enable controls. 
>   The background thread function then simply returns to cleanly close 
> the thread.  This may be helpful:
> 
> http://www.mvps.org/vcfaq/mfc/index.htm
> 
> -- 
> Scott McPhillips [VC++ MVP]
> 
> 
0
BrownViper (25)
5/18/2005 8:44:09 AM
BrownViper wrote:
> I m aware that my approach is awful, but what forced me to do it is the way 
> my application is designed:
> I have a Framewnd with 2 views on it, I use one for a visula feedback and 
> the other carrying a set of GUI controls i.e. buttons, textboxes, combo,...etc
> on to of that there are more GUI controls handled but the CDocument derived 
> object. Not forgetting the App menu and the min,max,close buttons.
> so to have a function to keep track of all these seems to me painfull and 
> most MFC gurus would pull out thier revolvers.
> that is why I used the CDialogThread approach.
> 
> Any comments on that would be appreciated
> 
> TIA 
> Viper

Congratulations for taking my "awful" comment constructively :)

When the main window displays a modal dialog then all controls and menus 
except the dialog's controls are disabled for you.  (This is done in one 
step by disabling the main window.)

A simple approach that avoids threading would be to call SetTimer and 
display a modal dialog.  Then do some of the background task within each 
OnTimer call.

-- 
Scott McPhillips [VC++ MVP]

0
Scott
5/18/2005 1:09:19 PM
Reply:

Similar Artilces:

popup dialogs in mfc activeX
Hi, I tried using PreModalDialog() and it didn't help. Let me attempt to explaining the scenario better. I am writing an ActiveX control. Class A is the control class that creates a socket and listens asynchronously. The message handler function for this Socket is a member function of Dialog A. So when there is a connection request from a client, the message handler function is invoked. I basically want to 1. accept the conection request. 2. read the incoming message. 3. popup a yes/no Dialog B 4. based on yes/no send true/false back to the client and disconnect the client. 5. Start as...

Not sure if Excel can do what i need
Hello all, I am obviously a new user to excel. I have a psychological assessmen that needs to be computerised for ease of use in obtaining results. Here is what i would like to happen. I have 28 questions with fiv Likert scale options(0-4) for answers. Each question addresses 1 of different personality traits. So questions 1, 6, 8, 22 look at anger. Where as questions 2, 9, 28 look at happiness. What i would like is fo excel to group the answers and give me the total number for each trait. Then based on that number, give out a prewritten (which i would provide result of the assessment. ...

Need Min Value from Row and Heading Value that Corresponds to it.
I have a table that looks somewhat like this w/o the *'d headings: h1 h2 h3 h4 h5 h6 h7 *Min Price *Heading $5.00 $6.00 $3.00 $4.00 $9.00 $1.00 $9.00 $1.00 h6 $3.00 $6.00 $8.00 $65.00 $9.00 $2.00 $4.00 $2.00 h6 $4.00 $9.00 $5.00 $7.00 $1.00 $2.00 $8.00 $1.00 h5 $9.00 $7.00 $6.00 $3.00 $9.00 $8.00 $2.00 $2.00 h7 $7.00 $8.00 $9.00 $7.00 $9.00 $8.00 $8.00 $7.00 h1 I have several SKU's with various price contract/programs. I want to find the best price in one cell and the heading that would correspond to that cell in another. I was trying to do this with an expression ...

Excel need help
I have 2 columns a and b . a contains payment b contains balance. I have the entire col b with the formula. my ? is when the balance appearsin b it is in all the cells in that column.I only want it in the used ones, not the unused ones:( example $270.00 16,730.00 $270.00 16,460.00 $100.00 16,360.00 $300.00 16,060.00 $270.00 15,790.00 15,790.00 15,790.00 15,790.00 15,790.00 15,790.00 this 15790.00 is in col b...not a...I want no bal in unused cells -- fiftieslady ------------------------------------------------------------------------ fiftieslady's Profile: http://www.excelforum.co...

Excel newbie needs help from math wiz (poker related)
I want to create a Excel workbook that will help in specific poker situations. Short version: I want to be able to calculate how often my opponent will need to fold for an all-in raise to show a positive expectation. Explanation: In poker you can win a hand by having the best hand at the end OR getting your opponent to fold. A common situation comes up where you have a "drawing hand" and face a bet by your opponent. In this situation you know you are currently behind in the hand, but have a chance to win if you catch some of your "outs" (cards that will give you th...

Need Help Linking Sheets
How can I link 2 pages without using the actual sheet as a reference? In other words, can I have two sheets linked by Identifying the same exact names one on each sheet? ...

HELP.....Setting up Outlook express again
I've just installed everything on my computer again and im having trouble with outlook express. its asking for my incoming and outgoing server names and i have no idea what they are or where i get them, so i can access or set my account up again. if anyone knows can they please help me thanks!!!! you can reach me at : bl182ink@hotmail.com Amanda Arbuckle what type of account? who is the email provider? have you checked their website for assistance? -- "Amanda" <bl182ink@hotmail.com> wrote in message news:A37A86BC-5E22-4B1F-B6E3-9459C70971C1@microsoft.com... > I...

Is there a way to go through all the controls on a MFC dialog?
hi, I need to get through all the controls in a MFC dialog without knowing their ID at design time. That mean that the program must get the id and then get the control. Is there a better solution? One thing is for sure, I have to get the CWnd* of all of the controls on a dialog. How do I do this? thanks ThunderMusic > I need to get through all the controls in a MFC dialog without knowing > their ID at design time. CWnd* pChild = MyDialog.GetWindow(GW_CHILD); while (pChild) { // your code... pChild = pChild->GetWindow(GW_HWNDNEXT); } thanks, it works... I had the GetWin...

Macro help needed........
How do I sort a particular column in ascending order and change the pag setup to landscape and margins to 0.25 using a macro code ? does anyone have a macro code for this ????? -- Message posted from http://www.ExcelForum.com Hi you have already received some answers in your previous threads please don't multipost! -- Regards Frank Kabel Frankfurt, Germany > How do I sort a particular column in ascending order and change the > page setup to landscape and margins to 0.25 using a macro code ? > > does anyone have a macro code for this ?????? > > > --- > Me...

Need to test a mobo
I had a power surge about 3 weeks ago - lost a drive, now making ticking noises. I had another two drive that seem to work sporadically now. So, I thought it might have been my power supply. Bought a tester and found out that all seems well. Next, I bought a new hard drive and found that it was also acting up - making noises like whirring up and down (like the other drives I thought were dead). So I am starting to wonder if it is my mobo - but how can I test this out? I am using a dual boot - Win XP and Win 7 - both drives work and don't work. Up and down. Right now my Win7 ...

Help needed
is there anyway you can see what links to other workbooks are associated with the workbook you have open. The problem i am having is when i open the workbook in question a message appears and states "The workbook you opened contains automatic links to information in another workbook. do you want to update this workbook with changes made to the other workbook" Yes....... No. if i click No the workbook opens, however if i click Yes the workbook just hangs and i have to go into task manager to shut down the system. I can only assume that there is a problem with one of the links....

Help with formula 12-17-09
Let's say I have in cell A1 a combo box with options: 1. Expense 2. Refund 3. Tax In cell B1 I will type a number In cell C3 I want a formula like this: If in cell A1 I have selected 1. Expense than calculate B1*25 If in cell A1 I have selected 2. Refund than calculate B1*50 If in cell A1 I have selected 3. Tax than calculate B1*150 How can I apply the combo box to entire column, like if I go to row 2 to find in B1 the combo box from A1 and in C2 the formula from C1 Thanks for the help In c1: =IF(A1="Expense",B1*25,IF(A1="Refund",B1*50,B1*150)) Micky ...

Need help on this....thanks
I have an Excel worksheet which stored a survey outcome of over 1200 members. There are over 30 questions in the survey and the first column captured the member ID. The problem is 3 of the survey questions that allow multiple answers, instead the answer for those three questions are captured in seperate column, the answers are stored in a single column and it makes it very difficult to analysis those result. As a result I created addittional columns depending on the total number of the answers available for that question and assign the result manually to that column. It is very time consuming ...

Clarification on EWS/Exchange 2007 free/busy issue
Clarification: Free/busy is denied to any user that has been granted specific access to a user's calendar when EWS is used to add/change a permission for a person or group. If the user is a member of a security group listed with access to the calendars, all users in the group will be denied access to free/busy to the calendar. We've discovered that when a user uses EWS (version 13.0.3) to modify calendar permissions (Exchange 2007) on his/her calendar that this causes an issue where the free/busy info is no longer available to any user listed/granted specific access to the u...

Help with Error
I got this error recently, and just want to make sure I'm reading it right. Logon Failure on database "First Storage Group\Mailbox Store (EXCHANGE)" - Windows 2000 account PARADIGM\user1; mailbox /o=Paradigm/ou=First Administrative Group/cn=Recipients/cn=user2. Error: -2147221231 Does this mean the user1 tried to access the user2 mailbox and failed? Thanks in advance hello albert, well, this looks familiar to: http://support.microsoft.com/default.aspx?scid=kb;en-us;260782 and http://support.microsoft.com/default.aspx?scid=kb;en-us;309113 regards, Alex "AlbertP&quo...

need help badly
How do you print ole objects.. Thanks lachanda -- Message posted via AccessMonster.com http://www.accessmonster.com/Uwe/Forums.aspx/access-forms/200710/1 On Wed, 03 Oct 2007 18:13:40 GMT, "misschanda via AccessMonster.com" <u36612@uwe> wrote: >How do you print ole objects.. >Thanks >lachanda What *kind* of OLE objects? Many of them are binary blobs (such as programs) which are inherently not printable. More details please! John W. Vinson [MVP] ...

I need Help
I have a random popup that i cant stop,when it does come up it seems to know what i was looking at,like when i was looking for registry cleaner it popped up and showed me one,any help would be great. On Jan 22, 6:57=A0am, Larry <La...@discussions.microsoft.com> wrote: > I have a random popup that i cant stop,when it does come up it seems to k= now > what i was looking at,like when i was looking for registry cleaner it pop= ped > up and showed me one,any help would be great. To eliminate questions and guessing, please provide additional information about your sys...

Stuck in review and need to be in design.
I am using Visio 2007. I need to complete the diagram that I have been working on and I hit the review key by mistake. How do I get back to the design screen? On Wed, 4 Nov 2009 16:14:01 -0800, cameron <cameron@discussions.microsoft.com> wrote: >I am using Visio 2007. I need to complete the diagram that I have been >working on and I hit the review key by mistake. How do I get back to the >design screen? menu Tools -> Track Markup and then close the Review window. -- Regards, Paul Herber, Sandrila Ltd. DFD/SSADM for Visio http://www.visio-dfd.sa...

newb-like question on threads
OK, so I thought I knew what a 'thread' was. But maybe not anymore... Basically, I just took the quick & dirty view that a thread was just a path of execution for an app. But anyway... I found this line of documentation for a function I wanted to use: "XXXEvent() does not return until the specified event occurs. For this reason, it is best used inside of a thread." Um... OK, newbiness now. How would I (not that I want to) get this function 'outside' of a thread? Thanks. djarvinen@gmail.com wrote: > OK, so I thought I knew what a 'thread' was. ...

Help, MVP!
Milly, Sue, Vince.. Can you help 'fix' this- I have still not found the cure, thank you! Outlook 2000 (Internet Only) is set to check for mail ever five minutes. However, when it does so, Outlook now insists on displaying the dialog box stating that the "Requested tasks were completed successfully." The dialog box has a "Hide this message" box which is checked, and the push pin is not 'set'. I must have messed up a setting when I reset my toolbars... (issue resolved - I accidentally pinned the toolbar to the desktop, went nuts till I figure out a doub...

help with a table
now that I set up a table through importing the data. I realized that I had some other data being emailed to me. how do i import it in that same table? On Fri, 29 Jun 2007 11:39:05 -0700, Maggie <schmidt.margaret@gmail.com> wrote: >now that I set up a table through importing the data. I realized that >I had some other data being emailed to me. how do i import it in that >same table? Use File... Get External Data... Link to link to a textfile containing the information from the EMail, and run an Append query to append it into your stored table. John W. Vinso...

Need Help Despertly!
I am in a desperate situation. I had to reformat my hard drive. Prior to do this, I copied my entire User Profile to another drive. I can now access my User Profile information from Windows Explorer. Now that I installed Microsoft Outlook, I do not see any of my Contacts. I need to restore my Contacts to the new Outlook installation. Again, I did save my entire "User Profile" from my previous installation of Windows 2000 Professional. I just do not know how to access my Contact information from Outlook that included addresses and phone numbers and such. Any help would be greatly appr...

Replicate free and busy
I am using the InterOrg Replication utility to replicate public folders and Free and Busy information. The public folders portion is working and one direction of the FB is working. My Exch5.5 users can view the FB schedules of my Exch2003 users. However, my Exch2003 users cannot view the Exch5.5 users schedules. I get "Unable to get collector from folder Ex:/o=organization /ou=site' on server [Exch2003]. I have created mail-enabled contacts on Exch2003 for some of my Exch5.5 users. And as per KB316022, they all have a legacyExchangeDN attribute - Mailbox /o=Exch55-org/...

Plug in needed
Looking for an Outlook plug in which, on reply to a e-mail, will:- a) allow selective quoting only, and b) not top quote. Anything around at all? Much to my horror (sorry guys) the Boss wants us to use Outofluck at work! -- On the carpool lane lane to Nirvana. John Phillips <flatulentdingo@deadspam.com> wrote: > Looking for an Outlook plug in which, on reply to a e-mail, will:- > > a) allow selective quoting only, and > > b) not top quote. > > Anything around at all? The best Outlook adjunct I've found for quote handling on replies is Outlook-Quotef...

What is the Need for XSD in Application Blocks designing
Can anyone help in clearing my doubt Hi, There is a need for me to implement Application Blocks in my project, (Basically 3 tire), the project comprises of UIL, BLL,Business Entities, DAL.. When i saw the example programs from User Interface Application Block example, which is implementing the Business Layer part, Interface for BLL and Business Entity part where by i am not able to understand the real usage of XSD's and the autogenerated C#? Can anyone explain me what is the process flow behind this fuda in a simple terms. Thanks in Advance Jagadeesh ...