Threading

I have a dialog application. I want use a button to start a thread. The
code is:

void CMyDlg::OnBnClickedButton1()
{
....
CWinThread *pThread = AfxBeginThread(ThreadProc, NULL);
....
}


UINT CMyDlg::ThreadProc(LPVOID pParam)
{
    DWORD result =0 ;

    // do somthing
    ...;

    AfxEndThread(0);
    return result;
}

when compile, I got "error C2665: 'AfxBeginThread' : none of the 2
overloads can convert parameter 1 from type 'UINT (LPVOID)'"

What is wrong?

0
yqin_99 (195)
11/8/2005 11:09:02 PM
vc.mfc 33608 articles. 0 followers. Follow

3 Replies
318 Views

Similar Articles

[PageSpeed] 42

kathy wrote:
> I have a dialog application. I want use a button to start a thread. The
> code is:

> UINT CMyDlg::ThreadProc(LPVOID pParam)
> {
>     DWORD result =0 ;
>
>     // do somthing
>     ...;
>
>     AfxEndThread(0);
>     return result;
> }
>
> when compile, I got "error C2665: 'AfxBeginThread' : none of the 2
> overloads can convert parameter 1 from type 'UINT (LPVOID)'"
>
> What is wrong?

Threads cannot be dynamic members of a class. For the method you chose,
you have two choices: Create a stand alone function, or create a static
member function. If you want to use the CMyDlg class, you can create
another non-static function, and then do something like follows

UINT CMyDlg::ThreadProc(LPVOID pParam)
{

if (pParam)
{
CMyDlg* pDialog = (CMyDlg*)pParam);
pDialog->DoStuff();
}

}

Then, when creating it, you just call:

AfxBeginThread(ThreadProc, this);

Note: Threading can cause fun little issues if you try to read and
write from the same variables in both threads. If you need more
information on this, I can help you with it.

0
darsant (187)
11/8/2005 11:20:43 PM
kathy wrote:

> What is wrong?

Did you define ThreadProc as static?
0
see9457 (7)
11/8/2005 11:22:43 PM
kathy wrote:
> I have a dialog application. I want use a button to start a thread. The
> code is:
> 
> void CMyDlg::OnBnClickedButton1()
> {
> ...
> CWinThread *pThread = AfxBeginThread(ThreadProc, NULL);
> ...
> }
> 
> 
> UINT CMyDlg::ThreadProc(LPVOID pParam)
> {
>     DWORD result =0 ;
> 
>     // do somthing
>     ...;
> 
>     AfxEndThread(0);
>     return result;
> }
> 
> when compile, I got "error C2665: 'AfxBeginThread' : none of the 2
> overloads can convert parameter 1 from type 'UINT (LPVOID)'"
> 
> What is wrong?
> 

In addition to Josh's excellent answer, you should know that 
AfxEndThread never returns.  It destroys the thread.  This means that 
the return statement is never executed, the "result" value is lost, and 
any stack allocated objects do not execute their destructors.  It is 
best to just eliminate the AfxEndThread call.

-- 
Scott McPhillips [VC++ MVP]

0
Scott
11/8/2005 11:35:43 PM
Reply:

Similar Artilces:

Problem with blocking of main thread with other thread
I have a problem with blocking of main thread: My application contains an interpreter and I need to write a debugger for it. The mainframe creates a thread that is interpreting instructions. When I need to break execution of instruction thread, I use WaitForSingleObject(m_WaitEventHandle, INFINITE); in the interpreter thread. Everything works fine until the window is opened from the interpreter. Then the mainframe freezes. When I break execution in VC the call stack for interpreter thread shows: � NTDLL! � KERNEL32! � CDebugManager::CheckBreakpoints() � CInterpreterThread::Run() � _Afx...

Where is my thread?
I posted a question about "Search for a value in multi-dimensionla array" an hour ago; where did that go? On 5/17/2010 17:38, Rick wrote: > I posted a question about "Search for a value in multi-dimensionla array" an > hour ago; where did that go? If you mean the one beginning with "Seach", posted almost half an hour before this one, it is there. :) On 5/17/2010 11:51 AM, Jackie wrote: > On 5/17/2010 17:38, Rick wrote: >> I posted a question about "Search for a value in multi-dimensionla >> array" an >> hour ag...

Diaglog/Threading Data Exchange Question
Greetings, I have learned a lot from all of you, and I hope to over come my latest road block. I have a smal IRC Chat client I am making. I have a Dialog Box with Control and I use a worker thread for connecting to IRC - That Thread will update controls on the Dialog by sending PostMessages. Now my problem is in the ListBox Control that I have extended to do popup menu with various commands. I need to be able to read that controls methods from within the Thread. so I need to do this from within my IRC Socket thread: (code not exact - pseudo code) CListBoxEx myUsers; //Defined in my ...

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(CDial...

Killing thread from outside thread in MFC App
Hi, Thanks to all those who offered advise on my last query. I successfully used the worker thread method to give control back to the dialog box whilst background processing was being carried out. The issue I have now is closing that thread externally. The method I've used to create the thread uses a volatile boolean variable. Whilever it is set to TRUE, the thread remains alive. However, when I set the variable to FALSE from the calling process (user wants to end processing), it has no effect. The thread continues to run. I thought that declaring the variable as 'volatile'...

Simple Thread
I have created a thread in my MFC Dialog based application, in the thread function i want to have access to a user interface control that is attached to the main dialog. How can I do it? Regards, Mystique Mystique wrote: > I have created a thread in my MFC Dialog based application, in the thread > function i want to have access to a user interface control that is attached > to the main dialog. > How can I do it? > > Regards, > Mystique > > Indirectly, by posting a message to the main thread asking it to update the control. http://www.mvps.org/vcfaq/mfc/12.ht...

To Thread or not to thread
I am using a CRecordset object to retrieve data from a remote server. The time to retrieve the data is highly variable because of network traffic etc etc etc and has varied from a few ms to almost a minute. Question: Is it a good practice to use a worker thread to handle the database exchange to avoid having my main thread block for long periods of time? I assume that I would let the exchange timeout and then check the return status and a global variable to determine if it should keep trying until it returned data ( might be null but that's ok). Ron H. Ron, While it may be a tin...

Anyone care to offer advice for my Client / Server threading model
I'm a bit of a sockets newbie and not too sure what the established ways of handling CAsyncSockets are. I need to handle two way comms between two or more handheld pcs and a server. WIth the server I've got: 1 Thread polling for new connections 1 Thread per client connection. The client thread is stuck in a loop called from CWinThread::InitInstance and sits and calls the CAsyncSocket::Receive function until something arrives. It uses peekmessage and postmessage in this loop to see if the app wants me to send anything to the client. This is working erratically though - peekmessage know...

multi-threaded control not destroyed on exiting the page?
I have created an ActiveX control (subclassing COleControl) which uses the afxRegFreeThreading model, and I have a small sample web page which inserts the control using an <object> tag. I noticed that when my page places the control inside the <head> tag, the control's destructor is called when my browser goes to a different page. But if the control is inside the <body> tag, the destructor is not called when I leave the page. The control's destructor is eventually called when I exit my browser, but I fear that my control is continuing to occupy system resources unt...

Threads in MFC
Hi to all... I've made a multithreaded Application (with MFC of course) and it works well on some PC, but in others PC's with the same OS (Win2000SP4) it throws "access Violation" immediatly just when a thread starts. (No matter which one... just the first). I've supposed it was some lib missing, and I've installed VC6... it didn't solve anything. any suggestion? I had a similar problem when I didn't realize I was trying to distribute a DEBUG version of the program. Otherwise, can you post a code snippet? On Mon, 19 Apr 2004 16:55:16 +0200, Nicola Bam...

MAD monitoring thread?
I have Exchange 2000 running on a Win2K Server. I have a trust to an NT4 domain with an Exchange 5.5 Server. Both are in the same site and my goal is to move the mailboxes and whatnot to the Exchange 2000 box. So far, just getting 2000 to operate has been a problem. It's installed and running but my MS Exchange Info Store and MTA stacks won't start in the services. If I reboot, they start, but stop within a few minutes. I have errors in my application log for MSExchangeSA that say, "The MAD monitoring thread was unable to read its configuration from the DS." I have MSEx...

A thread within a thread...
Hi, If i create a thread within a thread, something like... void threadA(void lparam ) { ... (HANDLE)_beginthread(ThreadB,0,someotherlparam); ... // do more work _endthread(); // done } My question is what happens to the thread i created, (threadB), if it is not finished? Is it safe, good?, to stop threadA without waiting for threadB. Many thanks Sims There is no concept of "a thread within a thread". All threads are within a process, and they are all peers of each other. DO NOT use _beginthread in an MFC program. This is asking for trouble. An...

why CStatic.m_hWnd not valid use realtime thread
Hi All I build an MFC app, and loadlibrary a dll which use high priority (realtime level ) threads for serial communication but after freelibrary the dll, some CStatic and CEdit objects not valid, their m_hWnd becomes to NULL How can I restore these CWnd objects? Is OS release them thanks, waiting online ...

threads #2
I have a server/client application that communicates using threads. The server class uses a while loop inside a thread to wait for new connections in accept state, like this: var a = new var( ); while( (server.Accept(connection)) ) { ...... ...... } I have declared a variable (var) right before this loop which uses memory. I want to free/delete this variable at the end of the while loop, but the control never reaches there. How can I make sure that this variable gets deleted before the server side exits? Thanks. Jessica Jessica Weiner wrote: > I have a server/client applicatio...

Destroy thread and Terminate thread
Hi, is there any difference between "Destroy thread and Terminate thread" regds vinay "Vinay" <v.sheel@mailcity.com> wrote in message news:uXR4ntHIEHA.2576@TK2MSFTNGP09.phx.gbl... > Hi, > is there any difference between > "Destroy thread and Terminate thread" > regds > > vinay > > A quote from the docs: "TerminateThread is a dangerous function that should only be used in the most extreme cases." destroyThread, on the other hand, deletes the thread object created by createThread. The thread should be terminated by some o...

Start background thread when a UI button is clicked, and Update UI button from background thread
I was asked these questions during a technical interview: 1) When the user clicks on a button on the form and that invokes a lot of heavy-duty calculations, what would you do? My answer: I would use "BeginInvoke" to start a background worker thread to do the heavy-duty calculations, in order to avoid blocking the main UI thread. Then he asked, "What does 'BeginInvoke' do"? I said it was a .NET pattern to start a background thread from a UI thread. Did I get it right? 2) Why can't you simply update a button on the UI thread from a background ...

Message Threads
Is there a way to organize a mail folder so that messages are organized similar to newsgroups? Based on whether or not the message is a reply? Thanks in advance for your help (Outlook 2003 in Windows XP Pro) Dave Have you tried the Arrange By Conversation view? -- Jocelyn Fiorello MVP - Outlook *** Messages sent to my e-mail address will NOT be answered -- please reply only to the newsgroup to preserve the message thread. *** In news:eYJDeGJ%23DHA.2212@TK2MSFTNGP10.phx.gbl, Dave wrote: > Is there a way to organize a mail folder so that messages are > organized similar to news...

how to get thread view in newsgroups?
I used to access newsgroups via Outlook Express where all messages within newsgroups were grouped in their threads with a collapse/expand plus or minus sign which was convenient and intuitive. Each one would indent according to which previous message was answered. Having moved to Windows Live Mail and accessing my newsgroups via this prog. now, I don't seem to be able to find the same kind of display option. All messages display independently in date order, and I can't find a way to group them in their normal thread except by entering something from the thread in the ...

My Threads
I just cant seem to figure out how to see all my postings since i started how do i see my own only thanks On Thu, 29 Apr 2010 11:27:01 -0700, Amin <Amin@discussions.microsoft.com> wrote: >I just cant seem to figure out how to see all my postings since i started how >do i see my own only thanks The Search function on the Microsoft webpage is broken; I've never been able to get it to work. What you can do instead is use a competitor's product: http://groups.google.com. Use Advanced Search to search for your posts, e.g. searching for amin@discussions.microso...

Thread Pool Class?
Can anyone recommend a good thread pool class? "Don Gurr" <dongurr@REMOVEshaw.ca> ha scritto nel messaggio news:2do6n.2339$RS6.61@newsfe15.iad... > Can anyone recommend a good thread pool class? Have you tried CThreadPool from ATL (I believe it can be used in MFC code as well)? http://msdn.microsoft.com/en-us/library/9tz6fz1e(VS.80).aspx HTH, Giovanni Thanks, it appears to work. "Giovanni Dicanio" <giovanniDOTdicanio@REMOVEMEgmail.com> wrote in message news:Ose%23WA7mKHA.2544@TK2MSFTNGP04.phx.gbl... > "Don Gurr"...

Off group subject but...how do MVPs follow threads ?s
I've been curious as to how a MVPs (or others with many posts and replies) keeps track of posts/threads? Do you download all threads? Or leave online and let your "newsreader" maintain all history? Or ____? I've always done the former. I manage all newsgroup activity in my mail program. I send a copy of my posts to myself and redirect a copy of all replies. I often, well sometimes ;), access those archived posts but it is somewhat time consuming process and I'm interested if there are more efficient alternatives. I've posted similar questions to t...

how to end a thread from the main thread
how to end a thread from the main thread Regards, Mystique Mystique wrote: > how to end a thread from the main thread Ending a thread directly from another thread should always be considered not an option. If you want to end a UI thread, post a WM_CLOSE message to it and let it terminate itself. If you want to end a worker thread, use a variable to signal to the thread that it's time to shut down. If you need to wait until the thread is shutdown, wait on it's handle in the main thread. Which type of thread are you using? Josh McFarlane See my essay on worker threads on...

problem with starting thread
During debugging if we go over the following line of code: m_pCalculateThread = AfxBeginThread(UpdateDocThread, this); the thread DOES NOT start, but if we go into AfxBeginThread function, the thread DOES start. Any suggestions? Thanks in advance! Maybe you can look in the threads window during the debug. You could set execution to a thread using that too. "Leonid Nikolayev" <lenikn@gmail.com> wrote in message news:68e06e88.0409090843.e1b6c45@posting.google.com... > During debugging if we go over the following line of code: > m_pCalculateThread = AfxBeginThread(Upd...

Worker threads acces to main dialog
Is it safe to pass a pointer to my main dialog app to worker threads and then have them call member functions like GetDlgItem etc? I will, of course, protect these calls with CCriticalSection. Thanks. DW. "dwizard" <daz-beck@tinyworld.co.uk> wrote in message news:ddq8pj$4op$1@nwrdmz03.dmz.ncs.ea.ibs-infra.bt.com... > Is it safe to pass a pointer to my main dialog app to worker threads and > then have them call member functions like GetDlgItem etc? > I will, of course, protect these calls with CCriticalSection. > No, only the thread that created a window shoul...

SendMessage for threads
Hi, This is not really an MFC question, and for that I apologize, but I am programming in MFC and I can't seem to find a better newsgroup to post this question to... Is there an equivilant functionality to SendMessage for a thread? I am using PostThreadMessage, but is there no way to specify that I want the calling thread to block until the thread being messaged has completed the task... just the same way that SendMessage does for windows? Or would that have to be implemented per thread implementation with syncronization objects? It sure would be nice if there were a gene...