473,406 Members | 2,273 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Why are threads so scary?

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 =---
Mar 13 '08 #1
1 1042
On Mar 13, 1:14 pm, Jeffro <jeffadamc...@hotmail.comwrote:
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.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
Mar 13 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Serge A. Ribalchenko | last post by:
Hi all, I'm new in python coding... Can someone point me how can I do such a usual thing? I need to ping some hosts, and harvest results in one list. Just cause there is about 120 hosts in the...
3
by: Ronan Viernes | last post by:
Hi, I have created a python script (see below) to count the maximum number of threads per process (by starting new threads continuously until it breaks). ###### #testThread.py import...
4
by: Joshua Moore-Oliva | last post by:
Ok.. so here is my situation. I have an application that I would love to be able to use asynchat with for it's handle_close, found_terminator etc abstraction on. However, I must use a separate...
0
by: Al Tobey | last post by:
I was building perl 5.8.2 on RedHat Enterprise Linux 3.0 (AS) today and noticed that it included in it's ccflags "-DTHREADS_HAVE_PIDS." I am building with -Dusethreads. With newer Linux...
10
by: [Yosi] | last post by:
I would like to know how threads behavior in .NET . When an application create 4 threads for example start all of them, the OS task manager will execute all 4 thread in deterministic order manes,...
3
by: mjheitland | last post by:
Hi, I like to know how many threads are used by a Threading.Timer object. When I create a Threading.Timer object calling a short running method every 5 seconds I expected to have one additional...
10
by: Darian | last post by:
Is there a way to find all the thread names that are running in a project? For example, if I have 5 threads T1, T2, T3, T4, T5...and T2, T4, and T5 are running...I want to be able to know that...
10
by: =?Utf-8?B?UHVjY2E=?= | last post by:
Hi, I'm using vs2005 and .net 2.0. I started 2 threadpool threads. How do I know when they're done with their tasks? Thanks. ThreadPool.QueueUserWorkItem(new...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.