Re: addstring to list box

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: bobby (bobby_at_discussions.microsoft.com)
Date: 11/15/04


Date: Mon, 15 Nov 2004 13:40:10 -0800

Hello there I have done this sample but the gui does not show anything xan
you help me.I have done what all you said in the previous discussions.
the code is a s follows

#include "stdafx.h"
#include "something.h"
#include "somethingDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CSomethingDlg dialog

CSomethingDlg::CSomethingDlg(CWnd* pParent /*=NULL*/)
        : CDialog(CSomethingDlg::IDD, pParent)
{
        //{{AFX_DATA_INIT(CSomethingDlg)
                // NOTE: the ClassWizard will add member initialization here
        //}}AFX_DATA_INIT
        // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
        m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CSomethingDlg::DoDataExchange(CDataExchange* pDX)
{
        CDialog::DoDataExchange(pDX);
        //{{AFX_DATA_MAP(CSomethingDlg)
        DDX_Control(pDX, IDC_LBOX, m_ctlListbox);
        //}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CSomethingDlg, CDialog)
        //{{AFX_MSG_MAP(CSomethingDlg)
        ON_BN_CLICKED(IDC_BCREATE, OnBcreate)
        //}}AFX_MSG_MAP
        ON_MESSAGE(UserMessage_DataReceived, DataReceived)//this also i have added
END_MESSAGE_MAP()

CWnd *wind = CWnd::FindWindow(_T("CSomethingDlg"),NULL);
HWND winds=(HWND )wind;
int a=0;//this is the global variable that needs to be modified by the threads
/////////////////////////////////////////////////////////////////////////////
// CSomethingDlg message handlers

LRESULT CSomethingDlg::DataReceived(WPARAM wParam, LPARAM lParam)
{

// here you are sure your thread received the message and you are ready to
//update your list control
        UpdateData(TRUE);
        m_ctlListbox.AddString((LPCTSTR)lParam);

return 0;

}

BOOL CSomethingDlg::OnInitDialog()
{
        CDialog::OnInitDialog();

        // Set the icon for this dialog. The framework does this automatically
        // when the application's main window is not a dialog
        SetIcon(m_hIcon, TRUE); // Set big icon
        SetIcon(m_hIcon, FALSE); // Set small icon
        
        CenterWindow(GetDesktopWindow()); // center to the hpc screen

        // TODO: Add extra initialization here
        
        return TRUE; // return TRUE unless you set the focus to a control
}

void CSomethingDlg::OnBcreate()
{
        // TODO: Add your control notification handler code here
        UINT somethread(LPVOID pParam);//he prototype of the thread controlling
function
        AfxBeginThread(somethread,0);//starting the thread worker here

        

}
UINT somethread(LPVOID pParam)
{
        while(a<1000)// the core reason for the thread creation
        {
                if(a==(990*a))//posts 10 messages to the gui updater func to display the
parameter a to be displayed on thegui
                        PostMessage(winds,UserMessage_DataReceived,a,a);//sending the parameter a
so be listed in the gui
         
else
{}
a=a+1;//incrementig the data index
        }//end of the while loop
return 1;
}//end of the threa dfunc

"Marius Prisecaru" wrote:

>
> "bobby" <bobby@discussions.microsoft.com> wrote in message
> news:14A29620-600C-4CF0-AAA4-A9D468A80BB3@microsoft.com...
> > ehello there.Can you tell me how to find the hwnd id of my dialog box(in
> my
> > app remember) from a thread other than using the find func as you said
> that
> > it would end up in a dead lock?
>
> Here is a quote from my answer in the "Thread on class," discussion:
>
> ===
> In the header of the class:
>
> static DWORD TheThreadFunction (LPVOID lpvoid);
> void MyThreadFunction();
>
> In the cpp you implement:
>
> DWORD TheThreadFunction(LPVOID lpvoid)
> {
> (CYourClass*) pThis = (CYourClass*)lpvoid;
> pThis->MyThreadFunction();
> return 0;
> }
> ====
>
> "CYourClass" in this case would be your dialog class. Then, in OnInitDialog
> you could start your thread like this:
>
> AfxBeginThread((AFX_THREADPROC)TheThreadFunction, this);
>
> So, from inside the MyThreadFunction() you could do:
>
> PostMessage(YourMessage, wParam, lParam);
>
> Note that you are not using a hWnd; this is because you are actually calling
> CWnd::PostMessage (rather that ::PostMessage) [since MyThreadFunction is
> part of your dialog class even though it runs in the worker thread!] As you
> can see, no need for hWnd in this scenario; your message will go to the
> right place -- that is, to your dialog.
>
> >I also would be grateful if you could tell me
> > how to post messages for updating(add an entry,search for an entry,delete
> an
> > entry) a combobox in my dialog based win app from my thread.
>
> All you have to do is post the messages and let our GUI's custom message
> handlers do all that. I already showed you how to define custom messages and
> how to have the GUI trap them.
>
> >
> > As far as I understood till now if i successfully postmessages with the id
> > ofmy window it should automatically do things(add the string that i pass
> to
> > the combo box,find whether a match is there in the combo box or
> not..).That
> > is a little ambiguous for me.the worker thread posts a message say to find
> > whether a matching string is there in the combobox or not.How will it get
> a
> > reply back if the search were successful or not?
>
> You can have a variable that is visible by both the GUI thread and the
> worker thread. Your thread would post a message to the GUI to do the checks,
> then the GUI would do the checks and update this variable... lastly, the
> thread would read this variable to know if the check was successful or not.
> You can use events to communicate to your worker threads... that is, you can
> "tell" your worker thread that the check is complete by setting an event and
> then the thread would react to this event and do whatever it would need to
> do. For this to happen, you would need to create the event [CreateEvent(..)]
> and have the worker thread respond to this event
> [WaitForSingle/MultipleObject(s).] In short, in the GUI you do the check,
> update the variable and call SetEvent(hEvent..); At this point, your worker
> thread would respond to hEvent and would check the variable to know whatever
> it needs to know.
>
> >
> > And is there a way to let the user select an item in the combobox and that
> > item to be used by another thread in the same program?
>
> Yes.. just do it as described above.
>
> Marius
>
>
>
>
>



Relevant Pages

  • Re: Problem with function!!
    ... you are trying to change the GUI from a worker thread. ... > for the flag changes.If it finds one,it would update the gui with the data ... > problem or error saying that GetDlgItemText unresolved external symbol,so ...
    (microsoft.public.windowsce.embedded.vc)
  • Re: MFC wait dialog
    ... hard drive and adds them to a listbox. ... I am aware of the fact I could use a worker thread for the ... Never show any GUI in the secondary thread -- just ... You can't have it both ways; you are saying you ...
    (microsoft.public.vc.mfc)
  • Re: MFC wait dialog
    ... I am aware of the fact I could use a worker thread for the ... Never show any GUI in the secondary thread -- just ... You can't have it both ways; you are saying you ... MVP Tips:http://www.flounder.com/mvp_tips.htm- Hide quoted text - ...
    (microsoft.public.vc.mfc)
  • Re: writing to gui from thread
    ... //this is the class of main gui with a listbox ... PostMessageusing the window handle. ... There's nothing wrong with calling AfxGetMainWndfrom a worker thread. ... it's not pointing to the same CWnd object that actually ...
    (microsoft.public.vc.mfc)
  • Re: Wait for Multiple objects
    ... GUI events. ... I do form a struct with event handles and hWnd that ... my 2 worker threads to post message to my GUI independently and they can ... pass a fairly long struct from worker thread A to worker thread B to ...
    (microsoft.public.windowsce.embedded)