c++/mfc 子线程结束后再来执行主线程下操作 该怎么处理
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c++/mfc 子线程结束后再来执行主线程下操作 该怎么处理相关的知识,希望对你有一定的参考价值。
if ( CUtility::IsMobile( strAccountUtf8.c_str() ) ) g_objIBodyService->isRegistered( stUserRegisterReq.mobile, GetHandle(), MSG_REGISTER_MOBILE_ISREGISTER); //return false; IBodyVerfCodeFrame verfFrame; verfFrame.setMobile( stUserRegisterReq.mobile ); if ( IDOK == verfFrame.DoModal( 436, 260, GetHandle() ) ) stUserRegisterReq.verfCode = verfFrame.verfCode(); else return true;
g_objIBodyService->isRegistered( stUserRegisterReq.mobile, GetHandle(), MSG_REGISTER_MOBILE_ISREGISTER); 这个会调用一个线程
咋等这个线程结束后 在执行下边的操作
if ( CUtility::IsMobile( strAccountUtf8.c_str() ) )
g_objIBodyService->isRegistered(stUserRegisterReq.mobile,GetHandle(),MSG_REGISTER_MOBILE_ISREGISTER);
//return false;
IBodyVerfCodeFrame verfFrame;
verfFrame.setMobile( stUserRegisterReq.mobile );
if ( IDOK == verfFrame.DoModal( 436, 260, GetHandle() ) )
stUserRegisterReq.verfCode = verfFrame.verfCode();
else return true;
g_objIBodyService->isRegistered(stUserRegisterReq.mobile,GetHandle(),MSG_REGISTER_MOBILE_ISREGISTER); 这个会调用一个线程
关注一个函数:WaitingForSingleObject,这个应该就是你需要的东西。本回答被提问者和网友采纳
Java主线程如何等待子线程执行结束(转)
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
|
</div> public class Threads { public static void main(String[] args) { SubThread thread = new SubThread(); thread.start(); //主线程处理其他工作,让子线程异步去执行. mainThreadOtherWork(); System.out.println( "now waiting sub thread done." ); //主线程其他工作完毕,等待子线程的结束, 调用join系列的方法即可(可以设置超时时间) try { thread.join(); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println( "now all done." ); } private static void mainThreadOtherWork() { System.out.println( "main thread work start" ); try { Thread.sleep(3000L); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println( "main thread work done." ); } public static class SubThread extends Thread{ @Override public void run() { working(); } private void working() { System.out.println( "sub thread start working." ); busy(); System.out.println( "sub thread stop working." ); } private void busy() { try { sleep(5000L); } catch (InterruptedException e) { e.printStackTrace(); } } } } |
- main thread work start
- sub thread start working.
- main thread work done.
- now waiting sub thread done.
- sub thread stop working.
- now all done.
1
2
3
|
public final void join() throws InterruptedException { join( 0 ); } |
public final synchronized void join(long millis)
1
2
3
|
while (isAlive()) { wait( 0 ); } |
Tests if this thread is alive. A thread is alive if it has been started and has not yet died.
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
|
public class Threads { static ExecutorService executorService = Executors.newFixedThreadPool( 1 ); @SuppressWarnings ( "rawtypes" ) public static void main(String[] args) throws InterruptedException, ExecutionException { SubThread thread = new SubThread(); // thread.start(); Future future = executorService.submit(thread); mainThreadOtherWork(); System.out.println( "now waiting sub thread done." ); future.get(); // try { // thread.join(); // } catch (InterruptedException e) { // e.printStackTrace(); // } System.out.println( "now all done." ); executorService.shutdown(); } private static void mainThreadOtherWork() { System.out.println( "main thread work start" ); try { Thread.sleep(3000L); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println( "main thread work done." ); } public static class SubThread extends Thread{ @Override public void run() { working(); } private void working() { System.out.println( "sub thread start working." ); busy(); System.out.println( "sub thread stop working." ); } private void busy() { try { sleep(5000L); } catch (InterruptedException e) { e.printStackTrace(); } } } } |
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
|
public class Threads { // static ExecutorService executorService = Executors.newFixedThreadPool(1); static final BlockingQueue queue = new ArrayBlockingQueue( 1 ); public static void main(String[] args) throws InterruptedException, ExecutionException { SubThread thread = new SubThread(queue); thread.start(); // Future future = executorService.submit(thread); mainThreadOtherWork(); System.out.println( "now waiting sub thread done." ); // future.get(); queue.take(); // try { // thread.join(); // } catch (InterruptedException e) { // e.printStackTrace(); // } System.out.println( "now all done." ); // executorService.shutdown(); } private static void mainThreadOtherWork() { System.out.println( "main thread work start" ); try { Thread.sleep(3000L); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println( "main thread work done." ); } public static class SubThread extends Thread{ private BlockingQueue queue; /** * @param queue */ public SubThread(BlockingQueue queue) { this .queue = queue; } @Override public void run() { try { working(); } finally { try { queue.put( 1 ); } catch (InterruptedException e) { e.printStackTrace(); } } } private void working() { System.out.println( "sub thread start working." ); busy(); System.out.println( "sub thread stop working." ); } private void busy() { try { sleep(5000L); } catch (InterruptedException e) { e.printStackTrace(); } } } } |
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
|
public class Threads { // static ExecutorService executorService = Executors.newFixedThreadPool(1); static final BlockingQueue queue = new ArrayBlockingQueue( 1 ); public static void main(String[] args) throws InterruptedException, ExecutionException { int threads = 5 ; CountDownLatch countDownLatch = new CountDownLatch(threads); for ( int i= 0 ;i<threads;i++){ SubThread thread = new SubThread( 2000 *(i+ 1 ), countDownLatch); thread.start(); } // Future future = executorService.submit(thread); mainThreadOtherWork(); System.out.println( "now waiting sub thread done." ); // future.get(); // queue.take(); countDownLatch.await(); // try { // thread.join(); // } catch (InterruptedException e) { // e.printStackTrace(); // } System.out.println( "now all done." ); // executorService.shutdown(); } private static void mainThreadOtherWork() { System.out.println( "main thread work start" ); try { Thread.sleep(3000L); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println( "main thread work done." ); } public static class SubThread extends Thread{ // private BlockingQueue queue; private CountDownLatch countDownLatch; private long work; /** * @param queue */ // public SubThread(BlockingQueue queue) { // this.queue = queue; // this.work = 5000L; // } public SubThread( long work, CountDownLatch countDownLatch) { // this.queue = queue; this .countDownLatch = countDownLatch; this .work = work; } @Override public void run() { try { working(); } finally { // try { // queue.put(1); // } catch (InterruptedException e) { // e.printStackTrace(); // } countDownLatch.countDown(); } } private void working() { System.out.println(getName()+ " sub thread start working." ); busy(); System.out.println(getName()+ " sub thread stop working." ); } private void busy() { try { sleep(work); } catch (InterruptedException e) { e.printStackTrace(); } } } } |
以上是关于c++/mfc 子线程结束后再来执行主线程下操作 该怎么处理的主要内容,如果未能解决你的问题,请参考以下文章