睡眠功能和线程 [英] Sleep function and threads

查看:87
本文介绍了睡眠功能和线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我需要帮助,他的睡眠功能如下。我需要写som

代码来做ff:


1.创建一个新主题

2.(in新线程),睡眠x毫秒

3.(在新线程中),经过一段时间后,通过回调调用一个函数

4.杀死该线程的能力产生的线程。

伪代码(来自可用的文档)如下:


//前向声明

routine()/ / sleep在这里调用

routine_callback()//时间到了什么要做


void foo(void){

if((h = CreateThread(..,routine,routine_callback,..& Id。)){

....

}

.......

//杀死线程

CloseHandle(h);

}

我想要一个工作示例,告诉我如何:


1)指定我的日常功能

2)。将回调函数和睡眠时间量传递给我的例程

函数

3)杀死产生的线程。


非常感谢

Hi,

I need help witht he sleep function as follows. I need to be write som
code to do the ff:

1. creates a new thread
2. (in the new thread), Sleep for x milliseconds
3. (in the new thread), After time elapsed, call a function via a callback
4. ability to kill the spawned thread.
Pseudocode (from the available docs) is as ff:

//Forward declarations
routine() //sleep is called in here
routine_callback() //something to do when times up

void foo( void ) {
if ((h =CreateThread( ..,routine,routine_callback,..&Id.)) {
....
}
.......
//Kill thread
CloseHandle(h) ;
}
I would like a working example that shows me how I can:

1) Specify my routine function
2). Pass a callback function and the sleep time amount to my routine
function
3) Kill the spawned thread.

Many thanks

推荐答案

> 1)指定我的日常功能


类似于: -


DWORD WINAPI例程(LPVOID lpParameter)

{

// ...

}
> 1) Specify my routine function

Something like :-

DWORD WINAPI routine( LPVOID lpParameter)
{
//...
}
2)。将回调函数和休眠时间量传递给我的例程
函数


CreateThread有一个LPVOID lpParameter。将指针传递给你的回调

函数作为lpParameter.And在你的线程proc(上面定义的例程)中,你

将lpParameter强制转换回你的回调函数指针类型

函数。

3)杀死生成的线程。


只需从线程proc返回(在本例中,上面定义的例程)


-

问候,

Nish [VC ++ MVP]
http:// www。 voidnish.com
http://blog.voidnish.com

" Alfonso Morra" < SW *********** @ the-ring.com>在留言中写道

news:db ********** @ nwrdmz01.dmz.ncs.ea.ibs-infra.bt.com ...
<我需要帮助,他的睡眠功能如下。我需要写som
代码来做ff:

1.创建一个新线程
2.(在新线程中),Sleep for x milliseconds
3.(在新线程中),经过一段时间后,通过回调调用一个函数
4.杀死生成的线程的能力。

伪代码(来自可用的文档)是ff:

//转发声明
routine()//在这里调用sleep
routine_callback()//时间到了要做什么
void foo(void){
if((h = CreateThread(..,routine,routine_callback,..& Id。)){
....
} > .......
//杀死线程
CloseHandle(h);
}

我想要一个工作示例向我展示我如何能够:

1)指定我的日常功能
2)。将回调函数和睡眠时间量传递给我的常规功能
3)杀死产生的线程。

非常感谢
2). Pass a callback function and the sleep time amount to my routine
function
CreateThread has an LPVOID lpParameter. Pass a pointer to your callback
function as lpParameter.And in your thread proc (routine defined above), you
cast lpParameter back to the function pointer type of your call back
function.
3) Kill the spawned thread.
Just return from the thread proc (in this case, routine defined above)

--
Regards,
Nish [VC++ MVP]
http://www.voidnish.com
http://blog.voidnish.com
"Alfonso Morra" <sw***********@the-ring.com> wrote in message
news:db**********@nwrdmz01.dmz.ncs.ea.ibs-infra.bt.com... Hi,

I need help witht he sleep function as follows. I need to be write som
code to do the ff:

1. creates a new thread
2. (in the new thread), Sleep for x milliseconds
3. (in the new thread), After time elapsed, call a function via a callback
4. ability to kill the spawned thread.
Pseudocode (from the available docs) is as ff:

//Forward declarations
routine() //sleep is called in here
routine_callback() //something to do when times up

void foo( void ) {
if ((h =CreateThread( ..,routine,routine_callback,..&Id.)) {
....
}
.......
//Kill thread
CloseHandle(h) ;
}
I would like a working example that shows me how I can:

1) Specify my routine function
2). Pass a callback function and the sleep time amount to my routine
function
3) Kill the spawned thread.

Many thanks






Nishant Sivakumar写道:


Nishant Sivakumar wrote:
1)指定我的日常功能

类似于: -

DWORD WINAPI例程(LPVOID lpParameter)
{
// ...
}

1) Specify my routine function

Something like :-

DWORD WINAPI routine( LPVOID lpParameter)
{
//...
}

2)。将回调函数和休眠时间量传递给我的例程
函数
2). Pass a callback function and the sleep time amount to my routine
function



CreateThread有一个LPVOID lpParameter。将指针传递给你的回调函数作为lpParameter.And在你的线程proc(上面定义的例程)中,你将lpParameter强制转换回你的回调函数指针类型
函数。


CreateThread has an LPVOID lpParameter. Pass a pointer to your callback
function as lpParameter.And in your thread proc (routine defined above), you
cast lpParameter back to the function pointer type of your call back
function.




我还需要通过睡眠时间。看起来CreateProcess API

只需要传递一个参数。这是否意味着我必须将我的回调函数和睡眠时间包装在某种ptr中,以及/或
(用户定义的参数)结构或类似的东西?



I also need to pass the sleep time. It looks like the CreateProcess API
only expects one parameter to be passed. Does this mean that I have to
wrap both my callback function and sleep time in some kind of ptr to
(user defined parameter) struct or something similar ?

3)杀死衍生的线程。
3) Kill the spawned thread.



我希望能够决定从
$ b $中杀死该线程b父线程。无论如何都要发送消息。 (我不是ma_ WM_ *

消息)到线程?这将是必要的,所以如果我将这个

代码移动到类的例子中,该线程可以在产生线程的父类的
的析构函数中被杀死。只需从线程proc返回(在本例中,上面定义的例程)


I want to be able to make the decision to kill the thread from the
Parent thread. Is there anyway to "send messages" (I don''t maan WM_*
messages) to the thread?. This will be necesary so that if I move this
code to class fo example, the thread can be killed in the destructor of
the parent that spawned the thread.

Just return from the thread proc (in this case, routine defined above)






>我还需要通过睡眠时间。看起来CreateProcess API
> I also need to pass the sleep time. It looks like the CreateProcess API
只需要传递一个参数。这是否意味着我必须将我的回调函数和睡眠时间包装在某种类型的ptr(用户
定义的参数)结构或类似的东西中?


是的,您可以使用如下结构: -


struct THREADDATA

{

FUNC_PTR fCallBack;

DWORD dwSlepTime;

};

我希望能够决定从线程中杀死线程家长
线程。无论如何都要发送消息。 (我不是mamin WM_ *消息)到线程?这将是必要的,所以如果我将此代码移动到类示例,则可以在生成线程的父级的析构函数中杀死该线程。


在MSDN上查找PostThreadMessage。


-

问候,

Nish [VC ++ MVP]
http://www.voidnish.com
http://blog.voidnish.com

" ; Alfonso Morra < SW *********** @ the-ring.com>在消息中写道

新闻:db ********** @ nwrdmz02.dmz.ncs.ea.ibs-infra.bt.com ...
Nishant Sivakumar写道:
only expects one parameter to be passed. Does this mean that I have to
wrap both my callback function and sleep time in some kind of ptr to (user
defined parameter) struct or something similar ?
Yep, you can use a struct like :-

struct THREADDATA
{
FUNC_PTR fCallBack;
DWORD dwSlepTime;
};
I want to be able to make the decision to kill the thread from the Parent
thread. Is there anyway to "send messages" (I don''t maan WM_* messages) to
the thread?. This will be necesary so that if I move this code to class fo
example, the thread can be killed in the destructor of the parent that
spawned the thread.
Look up PostThreadMessage on MSDN.

--
Regards,
Nish [VC++ MVP]
http://www.voidnish.com
http://blog.voidnish.com
"Alfonso Morra" <sw***********@the-ring.com> wrote in message
news:db**********@nwrdmz02.dmz.ncs.ea.ibs-infra.bt.com...

Nishant Sivakumar wrote:
1)指定我的日常功能
1) Specify my routine function



类似于: -

DWORD WINAPI例程(LPVOID lpParameter)
//



Something like :-

DWORD WINAPI routine( LPVOID lpParameter)
{
//...
}

2)。将回调函数和休眠时间量传递给我的例程
函数
2). Pass a callback function and the sleep time amount to my routine
function



CreateThread有一个LPVOID lpParameter。将指针传递给你的回调
函数作为lpParameter.And在你的线程proc(上面定义的例程)中,你将lpParameter强制转换回你的回调函数指针类型
函数。


CreateThread has an LPVOID lpParameter. Pass a pointer to your callback
function as lpParameter.And in your thread proc (routine defined above),
you cast lpParameter back to the function pointer type of your call back
function.



我还需要通过睡眠时间。看起来CreateProcess API只需要传递一个参数。这是否意味着我必须将我的回调函数和休眠时间包装在某种类型的ptr(用户
定义的参数)结构或类似的东西中?



I also need to pass the sleep time. It looks like the CreateProcess API
only expects one parameter to be passed. Does this mean that I have to
wrap both my callback function and sleep time in some kind of ptr to (user
defined parameter) struct or something similar ?

3)杀死生成的线程。
3) Kill the spawned thread.


我希望能够决定从Parent
线程中终止线程。无论如何都要发送消息。 (我不是mamin WM_ *消息)到线程?这将是必要的,所以如果我把这个代码移到类的例子中,线程可以在产生线程的父类的析构函数中被杀死。


I want to be able to make the decision to kill the thread from the Parent
thread. Is there anyway to "send messages" (I don''t maan WM_* messages) to
the thread?. This will be necesary so that if I move this code to class fo
example, the thread can be killed in the destructor of the parent that
spawned the thread.


只需从线程proc返回(在本例中,上面定义的例程)

Just return from the thread proc (in this case, routine defined above)



这篇关于睡眠功能和线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆