Bonjour,
lors du développement d'une solution, j'aurais besoin de passer une classe contenant mes paramètres (via pointeur) a mon thread secondaire actuellement voici ce que je fait (je poste juste une partie du code si besoin je mettrai le fichier complet)
deploy.cpp
Pour vous indiquez a quoi coresponde les variable voici le
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5 myParam.SetParam(m_pSettings, m_pComputerList, &m_FailedList); m_pThreadLauncher = AfxBeginThread((AFX_THREADPROC)InstallComputerList, &myParam, THREAD_PRIORITY_NORMAL,0,CREATE_SUSPENDED); m_pThreadLauncher->m_bAutoDelete = FALSE; m_pThreadLauncher->ResumeThread();
deploy.h
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 class CDeployingDlg : public CDialog { // Construction public: CDeployingDlg(CWnd* pParent = NULL); // standard constructor // Set agent settings to use for deploying, either Windows or Unix settings void SetAgentSetting( CAgentSettings *pSettings); // Set list of computer to setup void SetComputerList( CStringList *pList); // Dialog Data //{{AFX_DATA(CDeployingDlg) enum { IDD = IDD_DEPLOYING_DIALOG }; CSliderCtrl m_SliderThreads; CListBox m_ListBox; //}}AFX_DATA void StartUp(); // Overrides // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CDeployingDlg) protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support virtual LRESULT DefWindowProc(UINT message, WPARAM wParam, LPARAM lParam); //}}AFX_VIRTUAL // Implementation protected: // Message Handler allowing thread to update status and listbox content LRESULT OnMessageHandlerListbox( LPARAM lParam); LRESULT OnMessageHandlerStatus( LPARAM lParam); // Message Handler to allow launcher thread retreiving number of allowed threads LRESULT OnMessageHandlerGetMaxThreads(); // Agent settings to use for deploying, either Windows or Unix settings CAgentSettings * m_pSettings; // List of computer to setup CStringList * m_pComputerList; // List of computer where setup failed CStringList m_FailedList; // Thread used to launch setup thread for each computer CWinThread* m_pThreadLauncher; // Max simultaneous connections UINT m_uMaxThreads; };
enfin voici le processus appeler (toujoru dans deploy.cpp)
enfin voici CLauncherThreadParam.h
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120 UINT InstallComputerList( LPVOID pParam) { LPITEMIDLIST pMyIdList = NULL; LPMALLOC pIMalloc; CStdioFile myFile; CObArray threadHandles; // List of running threads CLauncherThreadParam* CLpParam = (CLauncherThreadParam *)pParam; try { CAgentSettings *pSettings = ((CLauncherThreadParam *)pParam)->GetSettings(); CStringList *pComputers = ((CLauncherThreadParam *)pParam)->GetComputersList(); CStringList *pFailed = ((CLauncherThreadParam *)pParam)->GetFailedList(); CString csComputer, // Computer to setup csSuccess, // String to format number of sucess or failure csLocalDir, // Local directory where to find setup.sh csFailure; POSITION pos; // Position into string list int nIndex, // Number of computer launched nThread; // Number of threads DWORD dwErr; CWorkerThreadParam myParam; // Parameters to pass to working threads CWinThread* pThread; // One working thread // Launch setup for each computer nIndex = 0; pos = pComputers->GetHeadPosition(); while (pos) { // Ensure not too many threads already running nThread = 10; while (threadHandles.GetSize() >= nThread) { // Too many threads lanuched, wait a while Sleep( 500); // Cleaning dead threads if needed nThread = 0; while (nThread < threadHandles.GetSize()) { // Get thread status pThread = (CWinThread*)threadHandles.GetAt( nThread); GetExitCodeThread( pThread->m_hThread, &dwErr ); if( dwErr != STILL_ACTIVE ) { // Clean thread threadHandles.RemoveAt( nThread); delete pThread; nThread --; } nThread ++; } nThread = 10; } // Launch new thread csComputer = pComputers->GetNext( pos); myParam.SetParam(pSettings, pFailed, csComputer, csLocalDir); printf("Host <%s:%i> Debut du deploiment\n", (CStringA)csComputer.GetBuffer(),pSettings->GetServerPort()); pThread = AfxBeginThread((AFX_THREADPROC)InstallComputer, &myParam, THREAD_PRIORITY_NORMAL,0,CREATE_SUSPENDED); threadHandles.Add( pThread); pThread->m_bAutoDelete = FALSE; pThread->ResumeThread(); // Set status text on dialogbox nIndex ++; csSuccess.Format( _T( "%d"), nIndex); csFailure.Format( _T( "%d"), pComputers->GetCount()); /*csComputer.FormatMessage( IDS_STATUS_RUNNING_DEPLOYMENT, csSuccess, csFailure); cout << csComputer << endl;*/ // Wait a while before launching next script Sleep( 1000); } // Waiting for threads to terminate //csComputer.LoadString( IDS_STATUS_STOP_DEPLOYMENT); // ::SendMessage( hWnd, WM_SETTEXT, IDC_MESSAGE_HANDLER_STATUS, (LPARAM) LPCTSTR( csComputer)); while (threadHandles.GetSize() > 0) { Sleep( 500); // Cleaning dead threads if needed nThread = 0; while (nThread < threadHandles.GetSize()) { // Get thread status pThread = (CWinThread*)threadHandles.GetAt( nThread); GetExitCodeThread( pThread->m_hThread, &dwErr ); if( dwErr != STILL_ACTIVE ) { // Clean thread threadHandles.RemoveAt( nThread); delete pThread; nThread --; //printf("Host <%s:%i> Fin du deploiment\n", (CStringA)csComputer.GetBuffer(),pSettings->GetServerPort()); } nThread ++; } } if (pFailed->GetCount() == 0) { // No error return 0; } else { //errors return 1; } } catch( CException *pEx) { return 1; } }
Donc le code compile parfaitement, l'exécution ne ose apparament pas de soucis si ce n'est le fait que le pointeur contient des données éronnées lors que le thread InstallComputeurList est appelé.
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 class CLauncherThreadParam { public: CLauncherThreadParam(); virtual ~CLauncherThreadParam(); void SetParam(CAgentSettings *pSettings, CStringList *pComputers, CStringList *pFailed); CAgentSettings * GetSettings(); CStringList * GetComputersList(); CStringList * GetFailedList(); protected: CAgentSettings *m_pSettings; CStringList *m_pComputers; CStringList *m_pFailed; };
Je ne comprend pas d'où viens l'erreur, j'ai fais essayer le code a un collègue bizarrement chez lui ca marche une fois sur deux (CF pièces jointes)
D'avance merci.
Mathieu.
Partager