Bonjour,
Je desire en apprendre un peu plus sur les thread. Pour cela j'ai decide de realiser un petit projet.
La realisation d'un chronometre.
Le chrono:
2 bouton:
- initialiser
- start/stop
1 edit:
- affichage du temps passer
Pour le moment je n'affiche que le temps en seconde(pas de millisecondes).
J'utilise GetTickCount() et AfxBeginThread(), trouver dans la FAQ:
http://c.developpez.com/faq/vc/?page=ProcessThread
Voici ma classe Dialog(je met que l'essentiel):
Maintenant l'implementation:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 // CChronoDlg dialog class CChronoDlg : public CDialog { // Construction public: CChronoDlg(CWnd* pParent = NULL); // standard constructor // Dialog Data enum { IDD = IDD_CHRONO_DIALOG }; static UINT myThread(LPVOID pvParam); CWinThread *m_pThread; protected: CEdit m_editChrono; DWORD m_tDebut; long m_hour; long m_min; long m_sec; public: afx_msg void OnBnClickedButtonInit(); afx_msg void OnBnClickedButtonStart(); };
Donc en fait je voudrai savoir si deja je commence bien et si j'utilise les bonnes methodes. Si je fais pas du n'imp...
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48 void CChronoDlg::OnBnClickedButtonInit() { m_pThread = NULL; pThis->m_editChrono.SetWindowTextA("00:00:00"); } void CChronoDlg::OnBnClickedButtonStart() { m_tDebut = GetTickCount(); m_pThread = AfxBeginThread(myThread, this); if(!m_pThread) { // Impossible de créer le thread ! return; } return; } UINT CChronoDlg::myThread(LPVOID pvParam) { CChronoDlg *pThis=reinterpret_cast< CChronoDlg *>( pvParam) ; DWORD tNow, tDiff; CString sTmp; while(1) { tNow = GetTickCount(); // La difference en seconde tDiff = (tNow - pThis->m_tDebut)/1000; // Les heures pThis->m_hour = tDiff/3600; tDiff -= pThis->m_hour*3600; // Les minutes pThis->m_min = tDiff/60; tDiff -= pThis->m_min*60; // Les secondes pThis->m_sec = tDiff; // On formate sTmp.Format("%02d:%02d:%02d",pThis->m_hour, pThis->m_min, pThis->m_sec); // On affiche pThis->m_editChrono.SetWindowTextA(sTmp); } return 0; }
Je n'en suis pas encore a la gestion des processus. Voici deja les petits problemes.
Le programme me prend tout mon pross(100%).
J'ai un clignotement rapide a l'affichage.
Merci
Partager