Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / March 2008

Tip: Looking for answers? Try searching our database.

Why are threads so scary?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Jeffro - 13 Mar 2008 01:14 GMT
I'm trying to learn threads so I created a simple dialog based MFC app
with an edit box, a Run button and an Exit button.  Run starts a loop
that continually updates the edit control.  How do I modify the code so
that the dialog responds to events?  That is, so the Exit button will
work.  Do I even need threads in this situation?

void CThreadPracticeDlg::OnBnClickedRun()
{   
    int i = 0;
    while (1) {
        if (i >= 10) {
            i = 0;
        }
        char intString[2];
        _itoa(i,intString,10);
        CString msg = CString(intString);           
        m_edit1.SetWindowTextW(msg);
        i++;       
        Sleep(500);
    }   
}

void CThreadPracticeDlg::OnBnClickedExit()
{   
    exit(0);
}

----== Posted via Pronews.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.pronews.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= - Total Privacy via Encryption =---
imuro - 13 Mar 2008 06:05 GMT
> I'm trying to learn threads so I created a simple dialog based MFC app
> with an edit box, a Run button and an Exit button.  Run starts a loop
[quoted text clipped - 27 lines]
> ----== Posted via Pronews.Com - Unlimited-Unrestricted-Secure Usenet News==----http://www.pronews.comThe #1 Newsgroup Service in the World! >100,000 Newsgroups
> ---= - Total Privacy via Encryption =---

Hi Jeffero,

I can see that your thread is running all the time since it is an
infinite loop. You are right that the UI and your internal program
should be on different thread so the UI is responsive even though your
loop is running. The exit button should stop the loop (thread) and
wait to see if the thread is finished, if it is then you exit
gracefully or  terminate thread and exit. Hope this helps :)

Cheers,
Chris

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.