|
i figured it out really simple actually
UINT CCash3mfcDlg::StartThread(LPVOID param)
{
CCash3mfcDlg *tony =static_cast<ccash3mfcdlg *="">(param);
tony->run();
return 1;
}
void CCash3mfcDlg::Ongobutton()
{
UpdateData(TRUE);
AfxBeginThread (StartThread,static_cast<lpvoid>(this));
}
in the run()
CString Tempstring;
///////////////converts the int to a string for the editbox control
Tempstring.Format( "%d", m_win ); m_winupdate.SetWindowText( Tempstring );
Anotherstring.Format( "%d", counter );
m_played.SetWindowText(Anotherstring);
Loststring.Format("%d", money);
m_lostvalue.SetWindowText(Loststring);
HOPE THIS HELPS SOMEONE ELSE IN THE FUTURE
Win32newb
"Making windows programs worse than they already are"
|
|
|
|
|
win32newb wrote:
updates an edit box based on the number of times the thread runs
I'm not exactly sure what you mean by that, but I looked at your code, and I assume you want to know how many times the thread goes through the loop. One problem I saw is that you need to stop thinking linearly with threads. You call your thread, and then the line UpdateData(TRUE); may or may not be executed before the thread...so you won't always get the same results. You should use some sort of control device, like a CSemaphore so that the Ongobutton() function waits until the thread is finished. Or another method is to have your thread update your edit box directly. I've never had assert problems with using threads...here is what I usually use if I plan on using variables from my class, otherwise I just pass NULL as my second parameter:
AfxBeginThread( StartThread, static_cast<lpvoid>(this) );
If you call your thread using this, you should have access to anything defined in the calling class, as well as your controls.
Good luck, and let me know if I was any help.
|
|
|
|
|
well i get a syntax error when i do what you stated.
i know in your example you use static_cast(this) _param is assigned this so i used it instead.
UINT CCash3mfcDlg::StartThread(LPVOID param)
{
THREADSTRUCT *ts = new THREADSTRUCT;
CCash3mfcDlg *pdlg;
pdlg = (CCash3mfcDlg*)CWnd::FromHandle(ts->_this);
pdlg->run();
//here is the time-consuming process which interacts with your dialog
return 1;
}
void CCash3mfcDlg::Ongobutton()
{
THREADSTRUCT *_param = new THREADSTRUCT;
_param->_this = GetSafeHwnd();
AfxBeginThread (StartThread,static_cast(_param)); // gives me error c2059: syntax error: '('
UpdateData(TRUE);
Win32newb
"Making windows programs worse than they already are"
|
|
|
|
|
Are you declaring your StartThread function as static in your class?
|
|
|
|
|
yes it is static. do you have like yahoo or something so it can be more realtime instead of playing message tag
in any event thanks for the help first of all
the portion of my header CCash3mfcDlg.h
static UINT StartThread (LPVOID param);
typedef struct THREADSTRUCT
{
HWND _this;
} THREADSTRUCT;
then here is how i have it implemented so far and not working
UINT CCash3mfcDlg::StartThread(LPVOID param)
{
THREADSTRUCT *ts = new THREADSTRUCT;
CCash3mfcDlg *pdlg;
pdlg = (CCash3mfcDlg*)CWnd::FromHandle(ts->_this);
pdlg->run();
//here is the time-consuming process which interacts with your dialog
return 1;
}
void CCash3mfcDlg::Ongobutton()
{
THREADSTRUCT *_param = new THREADSTRUCT;
_param->_this = GetSafeHwnd();
UpdateData(TRUE);
AfxBeginThread (StartThread,static(_param)); // gives me error c2059: syntax error: '('
}
int CCash3mfcDlg::run()
{
..........
}
Win32newb
"Making windows programs worse than they already are"
|
|
|
|
|
I don't have Yahoo, but I do use AIM. My screen name is DougW48 (go figure) if you need to ask any questions. I have found the cause of that error C2059, and I was able to compile your project without a problem. You need to stop passing _param as static:
AfxBeginThread( StartThread, _param );
Let me know if this fixes it for you, and if you're still having problems with those edit boxes! Good luck.
|
|
|
|
|
hello,
im quite new to all this.
i would like to make a toolbar/dialogbar in my program that will have hot buttons just like internet explorer has ("back", "next", "stop", "reload", "home".... etc.)
the hot button doesnt even have to be a different bitmap on it only that the button would "jump out" when the mouse is over it, and pressed..
(and of course the button background should be different)
can anyone help me or direct me to a tutorial about this?
thanks,
asher
http://www.backup2000.com
|
|
|
|
|
I'm very familiar with C/C++ Console Applications and Visual Basic programming. I'm having a tought time with Visual C++ Edit Boxes. I can't figure out how to format an editbox for hexadecimal data, input data and output data. I downloaded a few tutorials and demos on edit boxes, which were a great start for edit boxes in general, but none specifically address this topic.
Here is what I'm trying to do....
Supposing that you have a form with 3 edit boxes and a button. The user will input a 1 byte hexadecimal number into edit box1 and edit box2. When the user clicks the button, the hexadecimal values in edit box1 will be subtracted from the value in edit box2. The answer will be displayed in hexadecimal format in edit box3. One other thig is that I want to filter the input data "on the fly", only allowing the user to type the numbers and characters 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F into edit box1 and edit box2. Any other letters will be rejected and not accpted.
If anyone has some simple code examples that can help me I'd greatly appreciate it. Thanks..
Freddie.
|
|
|
|
|
I created a project with VS.2002 and do a lot of coding, now I want to change the exe file that compiler buid, where should I change the setting to create new exe file? I don't wanna create new project with new name. Any solution for it?
Mazy
"A bank is a place that will lend you money if you can prove that you don't need it." - Bob Hope
|
|
|
|
|
Mazdak wrote:
don't wanna create new project with new name
no please
I havent used VS.NET but i guess there must be a link tab somewhere in the project settings where you have to provide the outfile name. there you can change the name of the exefile name.
This space is empty.
|
|
|
|
|
Thanks, yes , it was somewhere in properties for project.
Mazy
"A bank is a place that will lend you money if you can prove that you don't need it." - Bob Hope
|
|
|
|
|
Project Properties|Linker|General|Output File
Anyone who thinks he has a better idea of what's good for people than people do is a swine.
- P.J. O'Rourke
|
|
|
|
|
Thanks.
Mazy
"A bank is a place that will lend you money if you can prove that you don't need it." - Bob Hope
|
|
|
|
|
Hello everyone
My question actually referrs to Borland C++ Builder 6.0. I'm trying to make a program minimize to the system tray using the TrayIcon sample from C++ Builder, but I get an error message:
"Cannot create system shell notification icon"
Can anyone, please, help me figure this out?
Thank you
|
|
|
|
|
http://www.bcbdev.com/faqs/faq73.htm
|
|
|
|
|
I have a worker thread i created in a dialog box. I want to run a thread till i either press the stop button or it finds a winnner. that part works fine. But i have an edit box called number of players with an int variable of m_win; I put inside the tread m_win=counter; UpdateData(FALSE); and the program will compile and run but as soon as it hits UpdateData(False); i get an assert error in reference to wincore.cpp 884. I read the information online about it and basically its saying either its a bug in vc++ or i'm coding it wrong. I think i'm coding it wrong cause i installed all the service packs. could someone please take the time and shed some light on this for me. If updatedata(false) can not be used how can i update my data? I know i can make a button that updates it but defeats the point.
thanks,
tony
////////////////cpp file for the dialog box////////////////
UINT CCash3mfcDlg::StartThread(LPVOID param)
{
THREADSTRUCT* ts = (THREADSTRUCT*)param;
ts->_this->run();
//here is the time-consuming process which interacts with your dialog
return 1;
}
void CCash3mfcDlg::Ongobutton()
{
THREADSTRUCT *_param = new THREADSTRUCT;
_param->_this = this;
AfxBeginThread (StartThread, _param);
UpdateData(TRUE);
}
int CCash3mfcDlg::run()
{
counter = 0;
bool win=false;
stop=false;
int random1,random2,random3;
m_processstatus.SetRange (1, 1000);
while (stop==false&&win==false)
{
m_win=counter;
Sleep(50);
random1 = rand()%5;
random2 = rand()%5;
random3 = rand()%5;
switch (random1)
{
case 0:
m_ball1.SetIcon(m_h0);
break;
case 1:
m_ball1.SetIcon(m_h1);
break;
case 2:
m_ball1.SetIcon(m_h2);
break;
case 3:
m_ball1.SetIcon(m_h3);
break;
case 4:
m_ball1.SetIcon(m_h4);
break;
}
switch (random2)
{
case 0:
m_ball2.SetIcon(m_h0);
break;
case 1:
m_ball2.SetIcon(m_h1);
break;
case 2:
m_ball2.SetIcon(m_h2);
break;
case 3:
m_ball2.SetIcon(m_h3);
break;
case 4:
m_ball2.SetIcon(m_h4);
break;
}
switch (random3)
{
case 0:
m_ball3.SetIcon(m_h0);
break;
case 1:
m_ball3.SetIcon(m_h1);
break;
case 2:
m_ball3.SetIcon(m_h2);
break;
case 3:
m_ball3.SetIcon(m_h3);
break;
case 4:
m_ball3.SetIcon(m_h4);
break;
}
if (m_ticket1==random1&&m_ticket2==random2&&m_ticket3==random3)
{
win=true;
}
m_processstatus.StepIt ();
counter++;
}
m_ticket3=1;
m_processstatus.SetPos(0);
//AfxEndThread(0);
CWnd::MessageBox("THREAD IS DONE");
return 0;
}
void CCash3mfcDlg::Onstopbutton()
{
stop=true;
}
//HEADER FILE FOR DIALOG BOX///////////////
// cash3 mfcDlg.h : header file
//
#if !defined(AFX_CASH3MFCDLG_H__B03A0082_F3ED_448F_A5AC_56734A264BF3__INCLUDED_)
#define AFX_CASH3MFCDLG_H__B03A0082_F3ED_448F_A5AC_56734A264BF3__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
/////////////////////////////////////////////////////////////////////////////
// CCash3mfcDlg dialog
class CCash3mfcDlg : public CDialog
{
// Construction
public:
CCash3mfcDlg(CWnd* pParent = NULL); // standard constructor
static UINT StartThread (LPVOID param); //controlling function header
typedef struct THREADSTRUCT //structure for passing to the controlling function
{
CCash3mfcDlg* _this;
} THREADSTRUCT;
// Dialog Data
//{{AFX_DATA(CCash3mfcDlg)
enum { IDD = IDD_CASH3MFC_DIALOG };
CEdit m_winupdate;
CStatic m_ball1;
CProgressCtrl m_processstatus;
CStatic m_ball3;
CStatic m_ball2;
int m_ticket1;
int m_ticket2;
int m_ticket3;
bool stop;
int m_draws;
int m_win;
int counter;
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CCash3mfcDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
HICON m_hIcon;
///////////////////added by me///////////////////
HICON m_h0;//ball zero icon
HICON m_h1;//ball one icon
HICON m_h2;//ball two icon
HICON m_h3;//ball three icon
HICON m_h4;//ball four icon
/////////////////////////////////////////////////
// Generated message map functions
//{{AFX_MSG(CCash3mfcDlg)
virtual BOOL OnInitDialog();
afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
afx_msg void OnDestroy();
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
afx_msg void Ongobutton();
afx_msg int run();
afx_msg void Onstopbutton();
afx_msg void Update();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_CASH3MFCDLG_H__B03A0082_F3ED_448F_A5AC_56734A264BF3__INCLUDED_)
|
|
|
|
|
|
Michael Dunn wrote:
It is dangerous to pass C++ objects from one thread to// another
If a pointer to the object is passed or a handle is passed to another thread. but still the two threads are sharing the same resource, so there is still bound to be problems either ways, rite ?
When it says C++ objects is it MFC C++ object or any other object?
This space is empty.
|
|
|
|
|
The comment means MFC objects. If you make up your own objects, you are responsible for making them thread-safe.
What creates the problem with MFC objects is that each thread has its own HWND<->CWnd association map. (This is used, for example, by CWnd::FromHandlePermanent() .) So once you pass a CWnd* to another thread, the thread doesn't have that object in its map, so it can't get to the HWND and back.
--Mike--
Personal stuff:: Ericahist | Homepage
Shareware stuff:: 1ClickPicGrabber | RightClick-Encrypt
CP stuff:: CP SearchBar v2.0.2 | C++ Forum FAQ
----
You cannot truly appreciate Dilbert unless you've read it in the original Klingon.
|
|
|
|
|
Thank you!
This space is empty.
|
|
|
|
|
I'm not asking you to do my work for me on this. However, how do i go about fixing it? How do i give it access to the hwnd?
I tried without luck this;
BOOL CCash3mfcDlg::OnInitDialog()
{
AFX_MANAGE_STATE(AfxGetStaticModuleState())
CMyDialog dlg(AfxGetMainWnd());
dlg.DoModal();
......
}
sorry trying to fix it just don't know how.
a small example would be great.
on that has a thread going on start and updates an edit box while it runs.
Win32newb
"Making windows programs run worse than they already do"
|
|
|
|
|
void CCash3mfcDlg::Ongobutton()
{
THREADSTRUCT *_param = new THREADSTRUCT;
_param->_this = this;
AfxBeginThread (StartThread, _param);
UpdateData(TRUE);
}
put it this way _param->_this = GetSafeHwnd();
and in the Run() proc
CYourDialog *pYourDlg;
*pYourDlg = (CYourDialog*)CWnd::FromHandle("the hwnd that was saved in the _param");
This space is empty.
|
|
|
|
|
I put this in and of course it doesn't work what am I doing wrong now.
errors i get:
C:\Downloads\cash3 mfc\cash3 mfcDlg.cpp(254) : error C2440: '=' : cannot convert from 'struct HWND__ *' to 'class CCash3mfcDlg *'
C:\Downloads\cash3 mfc\cash3 mfcDlg.cpp(268) : error C2065: '_param' : undeclared identifier
Error executing cl.exe.
cash3 mfc.exe - 2 error(s), 0 warning(s)
Thanks for your patience,
tony
void CCash3mfcDlg::Ongobutton() <br />
{<br />
<br />
THREADSTRUCT *_param = new THREADSTRUCT;<br />
_param->_this = GetSafeHwnd();<br />
<br />
<br />
AfxBeginThread (StartThread, _param);<br />
UpdateData(TRUE);<br />
<br />
<br />
<br />
<br />
}<br />
<br />
int CCash3mfcDlg::run()<br />
{<br />
CCash3mfcDlg *pdlg;<br />
pdlg = (CCash3mfcDlg*)CWnd::FromHandle(_param); <br />
<br />
<br />
m_processstatus.SetPos(0);<br />
<br />
<br />
AfxEndThread(0);<br />
CWnd::MessageBox("THREAD IS DONE");<br />
pdlg.UpdateData(FALSE);<br />
<br />
<br />
<br />
return 0;<br />
}<br />
Win32newb
"Making windows programs worse than they already are"
|
|
|
|
|
win32newb wrote:
_param->_this = GetSafeHwnd();
_this is defined as CCash3mfcDlg* make it HWND
pdlg = (CCash3mfcDlg*)CWnd::FromHandle(_param);
change it to
pdlg = (CCash3mfcDlg*)CWnd::FromHandle(_param->_this);
This space is empty.
|
|
|
|
|
THANKs for you help. that cleared that error but now i get one more
it says run() is not a member of HWND which its not. its a member of CCASH3MFCDLG
I tried changing ts->_this->run(); to the following
CCash3mfcDlg::run();
and i get an error
C:\Downloads\cash3 mfc\cash3 mfcDlg.cpp(239) : error C2352: 'CCash3mfcDlg::run' : illegal call of non-static member function
i tried this as well
typedef struct ANOTHERTHREAD //structure for passing to the controlling function
{
CCash3mfcDlg* _this;
} ANOTHERTHREAD;
and then
ANOTHERTHREAD* ts = (ANOTHERTHREAD*)param;
it compiled with no errors but as soon as i start the thread it crashes
Win32newb
if (user=="Win32newb")
{
CWnd::Messagebox
("DUH");
}
ELSE
{
CWnd::Messagebox
("HELP WIN32NEWB");
}
"Making windows programs worse than they already are"
|
|
|
|