Thread: Need help developing an application

  1. #16
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    >>I am having a compile error here m_ProgressBarDlg.Create(IDD_PROGRESSBAR_DLG,this);

    Looks like you are just trying to create a progressbar control.

    In the resource editor create a dialog with your progressbar on it. Derive a class from the dialog (CDialog).
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  2. #17
    Registered User
    Join Date
    Aug 2003
    Posts
    470
    There is one problem, I have here. If I replace 1000 by INFINITE in WaitForSingleObject() like this --> WaitForSingleObject( pStreamReadTask->m_hThread, INFINITE ); the application stops there.
    The wait is there to prevent the application thread exiting while the worker thread still exists. If the wait times out, maybe your worker thread never exits from its callback function. You need someway to signal the worker thread to exit. For instance, I have

    Code:
    volatile bool ReadWorker::stop_ = true;     // static variable in ReadWorker
    
    // Creates a thread using the run method as a callback.
    // Runs in the callee's thread.  
    CWinThread* ReadWorker::startThread()
    {
        stop_ = false;
        CWinThread* thread = AfxBeginThread(run, 0, THREAD_PRIORITY_NORMAL,
            0, CREATE_SUSPENDED);
    
        thread->m_bAutoDelete = false;
    
        thread->ResumeThread();
    
        return thread;
    }
    
    
    // Causes the thread to eventually exit
    void ReadWorker::stop()
    {
        stop_ = true;
    }
    And then in the thread's callback, the thread looks every so often at the stop_ variable. The thread's callback will eventually read changed value of stop_ and therefore exit its execution loop. When this happens, thread exit event is signaled.

    (Note: Microsoft doesn't recommend WaitForSignleObject INFINITE here: "A thread that uses a wait function with no time-out interval may cause the system to become deadlocked. Two examples of code that indirectly creates windows are DDE and COM CoInitialize. Therefore, if you have a thread that creates windows, use MsgWaitForMultipleObjects or MsgWaitForMultipleObjectsEx, rather than WaitForSingleObject." But I think the problem is more likely that the worker thread never exits its callback. I suspect the problems with WaitForSignelObject creating deadlocks either only occur at specific periods of time or occurs with a different processor arrangement. I haven't been able to get my test program to deadlock.)


    When I take out sleep(), the cpu usage reaches 100% and can't cancel the process.
    I don't think that should occur. The worker thread is mainly doing fileio and won't be doing any extensive computation.

    The code itself is kind of ugly with no error checking or exception handling. But if you want, PM me your email address and I'll send an email to you with the test program attached. The test program has a modal progress dialog, but uses a CEdit instead of a CProgressCtrl, and the files themselves are text files so it reads line by line and updates the dialog and view each time a line is read.

  3. #18
    Registered User Afrinux's Avatar
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    46
    Quote Originally Posted by okinrus
    The wait is there to prevent the application thread exiting while the worker thread still exists. If the wait times out, maybe your worker thread never exits from its callback function. You need someway to signal the worker thread to exit.
    I made a silly mistake. I didnt try to finish to the thread. With the following code, it works. Sorry for wasting your time.
    Code:
    	TerminateThread( pStreamReadTask->m_hThread, NULL );
    	WaitForSingleObject( pStreamReadTask->m_hThread, INFINITE );
    	delete pStreamReadTask;
    	pStreamReadTask = NULL;


    I don't think that should occur. The worker thread is mainly doing fileio and won't be doing any extensive computation.

    The code itself is kind of ugly with no error checking or exception handling. But if you want, PM me your email address and I'll send an email to you with the test program attached. The test program has a modal progress dialog, but uses a CEdit instead of a CProgressCtrl, and the files themselves are text files so it reads line by line and updates the dialog and view each time a line is read.
    Then, I am probably doing something wrong. Without the sleep() function, the user thread doesnt seem to be working.
    I will p.m you to give you my email addy. I really appreciate what you are doing for me.

    Quote Originally Posted by novacain
    >>I am having a compile error here m_ProgressBarDlg.Create(IDD_PROGRESSBAR_DLG,this);

    Looks like you are just trying to create a progressbar control.

    In the resource editor create a dialog with your progressbar on it. Derive a class from the dialog (CDialog).
    Thank you. I will try to do it. I was stuck there.

  4. #19
    Registered User Afrinux's Avatar
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    46
    Quote Originally Posted by novacain
    >>I am having a compile error here m_ProgressBarDlg.Create(IDD_PROGRESSBAR_DLG,this);

    Looks like you are just trying to create a progressbar control.

    In the resource editor create a dialog with your progressbar on it. Derive a class from the dialog (CDialog).
    I have tried to follow your instructions, but I still have the same problem:

    1. I created a dialog( enum { IDD = IDD_PROGRESS_BAR }; )
    2. I put a progress bar on it
    3. I created a class like this:
    Code:
    class CProgressBar : public CDialog
    {
    public:
    ...
    	enum { IDD = IDD_PROGRESS_BAR };
    	CProgressCtrl	m_ProgressBar;
    .....
    private:
    };
    4. I created a function to show the Dialog
    Code:
    BOOL CProgressBar::showPgrBarDlg( void )
    {
    	//check to see if the Progressbar Dlg has already been created
    	if( m_ProgressBar.GetSafeHwnd())
    		m_ProgressBar.ShowWindow(SW_SHOW);
    	else
    		m_ProgressBar.Create( IDD_PROGRESS_BAR, this );  
    	//make sure visible
    	m_ProgressBar.ShowWindow(SW_SHOW);
    }
    Unfortunately, the same compile error keeps coming out.
    Any idea what may be the causes?
    Thanks in advance.
    Afrinux

  5. #20
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Ahh...
    In general, you want to create a (member) dlg, using your derived class, as a child of another dialog/window.

    You need to add a member variable of your class 'CProgressBar' to the window that wants to show/use the progressbar dlg (not in the ProgressBar dlg class itself).
    ie add a member of CProgressBar to the main window.

    Then call my 'show' code in the window that wants to show/use the progressbar dlg (not in the CProgressBar class).

    Thats why you are getting the error, it is trying to create a CProgressBarCtrl (which has 4 params)

    >>CProgressBar
    not the best name as it might confilct with a named class.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  6. #21
    Registered User Afrinux's Avatar
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    46
    Hi novacain! Thanks for your help. Now it works.
    Code:
    BOOL CDVDVideoSolutionDlg::showPrgBarDlg( void )
    {
    	clPrgBar = new CProgressBar();
    	if( clPrgBar->GetSafeHwnd())
    		clPrgBar->ShowWindow(SW_SHOW);
    	else
    		clPrgBar->Create( IDD_PROGRESS_BAR, this );  
    
     #if 1// center the dialog box
    	CRect rectMain;
    	GetClientRect(&rectMain);
    	int xMain = rectMain.Width();
    	int yMain = rectMain.Height();
        // Get dimensions of the dialog
    	CRect rectView;
    	clPrgBar->GetWindowRect(&rectView);
    	int xView = rectView.Width();
    	int yView = rectView.Height();
    	// Calculate the (x,y) coordinate for Progress bar dialog.
    	int x = (xMain - xView) / 2;
    	int y = (yMain - yView) / 2;
    
    	// Move the dialog.
    	clPrgBar->MoveWindow(x, y, xView, yView, TRUE);
    #endif
    
    	//make sure visible
    	clPrgBar->ShowWindow(SW_SHOW);
    	
    	return TRUE;
    }
    Now, I have only 2 problems:
    1. Center the Progress bar dialog; the code above was supposed to do it, somehow the dialog is not centered.

    2. Get the sleep() function off the code

    >>CProgressBar
    not the best name as it might confilct with a named class.
    Thank you for the warning, I will take it into consideration from now on.
    Afrinux

  7. #22
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Try adding the style, DS_CENTER to the progressbar dialog.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  8. #23
    Registered User Afrinux's Avatar
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    46
    Quote Originally Posted by novacain
    Try adding the style, DS_CENTER to the progressbar dialog.
    I got it working with this:
    clPrgBar->CenterWindow( this );
    Thanks. By the way, are a Man Utd fan?

  9. #24
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    >>By the way, are a Man Utd fan?

    No but got to admire a man who knows what is important in this life.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  10. #25
    Registered User Afrinux's Avatar
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    46
    Quote Originally Posted by novacain
    >>By the way, are a Man Utd fan?

    No but got to admire a man who knows what is important in this life.
    I second that. Thanks again for every thing.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help for developing an application
    By Afrinux in forum C++ Programming
    Replies: 4
    Last Post: 03-03-2006, 02:03 AM
  2. Problem with com application
    By amardon in forum C++ Programming
    Replies: 3
    Last Post: 10-06-2005, 05:50 AM
  3. MFC run application by clicking on file...
    By dug in forum Windows Programming
    Replies: 4
    Last Post: 12-02-2004, 04:33 AM
  4. Developing TCP/IP Application in Microsoft Visual Studio VC++
    By ashu12 in forum Windows Programming
    Replies: 1
    Last Post: 09-11-2002, 03:30 AM