|
1 /* |
|
2 * Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 #include "stdafx.h" |
|
22 #include "STATTask.h" |
|
23 |
|
24 ////////////////////////////////////////////////////////////////////////// |
|
25 // Constructor |
|
26 // Takes 3 parameters: |
|
27 // 1) Pointer to function to start the thread in |
|
28 // 2) Pointer to class object to use within the thread |
|
29 // 3) Optional string identifier |
|
30 ////////////////////////////////////////////////////////////////////////// |
|
31 STATTask::STATTask(const ThreadProc pFunc, void *pMember, char *pConnection) |
|
32 { |
|
33 pfThreadProc = pFunc; |
|
34 theMember = pMember; |
|
35 szConection = NULL; |
|
36 |
|
37 if (pConnection) |
|
38 { |
|
39 szConection = new char[strlen(pConnection) + 1]; |
|
40 if (szConection) |
|
41 strcpy(szConection, pConnection); |
|
42 } |
|
43 } |
|
44 |
|
45 |
|
46 ////////////////////////////////////////////////////////////////////////// |
|
47 // Destructor |
|
48 STATTask::~STATTask() |
|
49 { |
|
50 Kill(); |
|
51 |
|
52 if (szConection) |
|
53 delete [] szConection; |
|
54 } |
|
55 |
|
56 |
|
57 ////////////////////////////////////////////////////////////////////////// |
|
58 // Create the thread |
|
59 bool STATTask::Start() |
|
60 { |
|
61 bool valid = true; |
|
62 |
|
63 // event attributes for the child process |
|
64 SECURITY_ATTRIBUTES eventAttr; |
|
65 eventAttr.nLength = sizeof(eventAttr); |
|
66 eventAttr.lpSecurityDescriptor = NULL; |
|
67 eventAttr.bInheritHandle = TRUE; |
|
68 |
|
69 // spawn a thread to do the processing |
|
70 DWORD dwThreadID; |
|
71 if (!(hThreadHandle = CreateThread( NULL, // security attributes |
|
72 0, // stack size |
|
73 pfThreadProc, // proc to call |
|
74 theMember, // proc parameter |
|
75 0, // creation flags |
|
76 &dwThreadID))) // thread identifier |
|
77 { |
|
78 valid = false; |
|
79 } |
|
80 |
|
81 return valid; |
|
82 } |
|
83 |
|
84 |
|
85 ////////////////////////////////////////////////////////////////////////// |
|
86 // Check to see if thread has finished processing |
|
87 bool STATTask::StillActive(DWORD timeout) |
|
88 { |
|
89 bool valid = false; |
|
90 |
|
91 if (hThreadHandle) |
|
92 { |
|
93 if (WAIT_TIMEOUT == WaitForSingleObject(hThreadHandle, timeout)) |
|
94 { |
|
95 valid = true; |
|
96 } |
|
97 } |
|
98 |
|
99 return valid; |
|
100 } |
|
101 |
|
102 |
|
103 ////////////////////////////////////////////////////////////////////////// |
|
104 // Kill the thread |
|
105 bool STATTask::Kill() |
|
106 { |
|
107 bool valid = true; |
|
108 |
|
109 if (hThreadHandle) |
|
110 { |
|
111 if (!TerminateThread(hThreadHandle, 0)) |
|
112 { |
|
113 valid = false; |
|
114 } |
|
115 |
|
116 hThreadHandle = 0; |
|
117 } |
|
118 |
|
119 return valid; |
|
120 } |
|
121 |