|
It's true for any object that is not thread-safe.
"One must learn from the bite of the fire to leave it alone." - Native American Proverb
|
|
|
|
|
Because you are going to be doing complex things, you should consider using a user-interface thread. Just using a single thread function (threadFunc as you described) is considered a worker thread. I have recent experience with them, and here's what you need.
1. Derive a class from CWinThread that will be used
2. Add any pointer or whatever you want to the class definition
3. Override the OnIdle function (may be automatically overridden)
a. put all functionality in there
b. return a nonzero value if you need more CPU time, return 0 if the thread is finished
4. In your main function where the thread will start, the thread will be started like this:
CMyThread * thread = (CMyThread *) AfxBeginThread (RUNTIME_CLASS(CMyThread), THREAD_PRIORITY_IDLE, 0, CREATE_SUSPENDED, NULL);<br />
<transfer data to the thread to be used by the thread here><br />
thread->ResumeThread();
At this point, the thread will be running.
Note: you will not create your own instance of the thread, the AfxBeginThread does all the creation of the data you need. Also, put whatever data that you would normally put in the constructor and destructor, put them in InitInstance and ExitInstance respectively. The threads constructor and destructor
Note: the thread written above will be created but will not be started automatically, hence the CREATE_SUSPENDED option. This gives you a time to pass as much data to the thread as you need. Run ResumeThread() to start the thread. At this point, the InitInstance member function will be run and can do the work of the constructor. Then the OnIdle member function will continually be called repeatedly until there is either a message to handle, or you return 0 and tell the thread no more work is needed. Message passing is considered a vital but relatively uncommon task so it's given a high priority. Since message passing is uncommon, the thread will consume practically it's entire time doing your work but it still needs to occaisionally checked and dispatched messages if needed.
You can use AfxEndThread() to stop the thread inside the threads' functions, and ExitInstance will be run to do the work of the destructor.
I've used worker threads and unless you need pure work done and aren't concerned with any message passing or anything else besides pure work, then UI threads provide many benefits.
|
|
|
|
|
Thanks a lot LighthouseJ, I will test it when I get home. I'm at work and steeling a few time to resolve a private life problem. XD
=^_______^=
[www.bispenho.com]
|
|
|
|
|
I am using some object refrence and boolean variables, these are declared golbally. I know this isn't a good practice but, it is required.
I want to know Can i use nampespace to store these globals and is it of any importance.
Also I don't know how to create and use namespace.
Can you help me plz.
|
|
|
|
|
yes, you can put them in a namespace, and it's good style to do so.
to declare:
namespace myNameSpace
{
bool globalBool;
CMyObject globalObject;
...
};
...
to use:
myNameSpace::globalBool = false;
Cleek | Image Toolkits | Thumbnail maker
|
|
|
|
|
|
Just a little addition to previous replies , if you don't want to write
myNameSpace::SomeFunction() every time you call it, then you can add following line to your main() function :
using namespace myNameSpace;
And then you will be able to directly call SomeFunction() in main();
"Success is the ability to go from one failure to another with no loss of enthusiasm." - W.Churchill
|
|
|
|
|
karmendra_js wrote:
Also I don't know how to create and use namespace.
Name Space[^]
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
cheers,
Alok Gupta
VC Forum Q&A :- I/ IV
|
|
|
|
|
Hi,
Yes you can locate global variables in namespaces.
The importance of doing so depends on the specific case.
For example, if you are afraid your var's name could
conflict with another var's name, put yours in a namespace.
Another example, if you have many global variables (hope
you don't) you'd rather put them in a namespace in terms of
'well organization'.
Code:
namespace Globals
{
bool bVar1;
bool bVar2;
// any others...
} // namespace Globals
Now let's use it:
// method 1. (not preferred)
using namespace Globals;
void f()
{
bVar1 = false;
}
// method 2. (less preferred)
using Global::bVar1
void f()
{
bVar1 = false;
}
// method 3. (ok)
void f()
{
Globals::bVar1 = false;
}
|
|
|
|
|
Hi,
I want to disable multiple row select in a list control. It should select only one row at a time how can i do this.
Thanks
|
|
|
|
|
In design view select list control properties->styles->single selection
check this box , u r job is done !
Muthu Ramji.Al.V
Software developer(VC++)
Sify Limited
muthu_ramji@sifycorp.com
|
|
|
|
|
I want to do it programatically. It is in LVS_REPORT style.
What you are saying is for ListBoxCtrl.
Thanks
|
|
|
|
|
Dear friend,
if u have 2 do in code then try this,
1.
//-------- Start create a list control using dwstyle LVS_SINGLESEL
CListCtrl* pmyListCtrl=new CListCtrl;
pmyListCtrl->Create(WS_CHILD|WS_VISIBLE|WS_BORDER|LVS_REPORT|LVS_SINGLESEL ,CRect(10,10,300,200), this, 1);
//---------
2.same can be done with visual design mode as i said earlier. Both in list box & list control v have that check box. Kindly check in property dialog box
3.if u want i will help with a demo program
|
|
|
|
|
does somebody knows how to use this function
I know that it gets 7 param ,
but the weird thing is that theres is no other function to receive the File
something like ReceiveFile , there is no other function, sooo
how can I receive the transmited file ??????????????????
so how can I receive the file ??
I need to transfer one file through a sockte,,
the socket is working right , I can send and receive with functions
send () , and recv ()
but I want to know the ease way to transfer a file,,
can somebody help me ???
THANX
GOD BLESS YA
REU
|
|
|
|
|
I meet problem about read and write file
my email is fisheryj@gmail.com
----------------------------
Jerry yu
Chinese
programming fans
|
|
|
|
|
|
|
Jerry yu wrote: I meet problem about read and write file
And what exactly would that problem be?
"One must learn from the bite of the fire to leave it alone." - Native American Proverb
|
|
|
|
|
I use vc++ just several times .I use Delphi before .
So I hope some one to help me .Because mfc is too difficult for me
thx
----------------------------
Jerry yu
Chinese
programming fans
|
|
|
|
|
Which function can I use in VC6?
Prompt : there are not the CHS parameters for USB flash disk .
|
|
|
|
|
I have used a date time control in my
project which i need to get the date from
teh user ,
I get everthing fine but the date retrived
is wrong.When again i select the same date
i get the date write by the same code .
Has anyone faced such problem.
Vikas Amin
Embin Technology
Bombay
vikas.amin@embin.com
|
|
|
|
|
I couldnt exactly get your problem. Maybe you should call UpdateData(FALSE) before using the associated variable.
"Do first things first, and second things not at all."
— Peter Drucker.
|
|
|
|
|
The NULL Developer wrote:
Maybe you should call UpdateData(FALSE) before using the associated variable.
Just a little Correction.. there is no need of use UpdateData(FALSE) on CDateTimeCtrl
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
cheers,
Alok Gupta
VC Forum Q&A :- I/ IV
|
|
|
|
|
Thank you Alok
in the MSDN samples just search for floder named "cmnctrl1"
open the project and in the file "datetime.cpp" replace the function as below
<br />
<br />
<br />
void CDateTimePage::OnDTNotify(NMHDR *pHdr, LRESULT * pRes)<br />
{<br />
CTime ct;<br />
CString st;<br />
<br />
switch( pHdr->code)<br />
{<br />
<br />
case DTN_CLOSEUP:<br />
m_lbNotifyList.AddString(_T("DTN_CLOSEUP"));<br />
<br />
break;<br />
case DTN_DATETIMECHANGE:<br />
<br />
m_lbNotifyList.AddString(_T("DTN_DATETIMECHANGE"));<br />
(m_DateTime.GetMonthCalCtrl())->GetCurSel(ct);<br />
ct.GetMonth ();<br />
m_DateTime.UpdateData (TRUE);<br />
st.Format(_T(" --- %02d/%02d/%2d ---%2d"),ct.GetMonth(),ct.GetDay(),ct.GetYear(),((ct.GetDay())-12 ));<br />
m_lbNotifyList.AddString(st);<br />
break;<br />
<br />
case MCN_SELECT:<br />
m_lbNotifyList.AddString(_T("asdf"));<br />
break;<br />
<br />
case DTN_DROPDOWN:<br />
m_lbNotifyList.AddString(_T("DTN_DROPDOWN")); <br />
break;<br />
case DTN_FORMAT:<br />
m_lbNotifyList.AddString(_T("DTN_FORMAT"));<br />
break;<br />
case DTN_FORMATQUERY:<br />
m_lbNotifyList.AddString(_T("DTN_FORMATQUERY"));<br />
break;<br />
case DTN_USERSTRING:<br />
m_lbNotifyList.AddString(_T("DTN_USERSTRING"));<br />
break;<br />
case DTN_WMKEYDOWN:<br />
m_lbNotifyList.AddString(_T("DTN_WMKEYDOWN"));<br />
break;<br />
}<br />
<br />
}<br />
The code in between the //->---- ... ... . //->-----
is added by me actully according to the theory it should display
the data in the datetime control but it displays a wrong date that
is 12 days before .
I found a solution to directly get the text from the date time control
with GetWindowText ();
but i have to parse it by my self & cannot use the direct function like GetMonth(),GetDay(),GetYear(),etc.
Vikas Amin
Embin Technology
Bombay
vikas.amin@embin.com
|
|
|
|
|
vikas amin wrote:
I found a solution to directly get the text from the date time control
with GetWindowText ();
What about calling
DWORD CDateTimeCtrl::GetTime(
CTime& timeDest
) const;
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
cheers,
Alok Gupta
VC Forum Q&A :- I/ IV
|
|
|
|