C ++ setTimout函数? [英] C++ setTimout function?

查看:125
本文介绍了C ++ setTimout函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++中像setTimeout-function这样的JavaScript最便宜的方式是什么?

What's the cheapest way for a JavaScript like setTimeout-function in C++?

我需要这个:
5000毫秒从现在开始,函数xy没有参数,没有返回值)。

I would need this: 5000 miliseconds from now, start function xy (no parameters, no return value).

这是因为我需要初始化COM的文本到语音,但是当我做的DLL附加,它崩溃。

The reason for this is I need to initialize COM for text to speech, but when I do it on dll attach, it crashes.

然而,如果我不从dllmain调用CoInitialize,它工作正常。

It works fine however if I do not call CoInitialize from dllmain.

我只需要调用CoInitialize CoCreateInstance,然后在其他函数中使用该实例。我可以通过检查NULL捕获未初始化的实例,但我需要初始化COM - 不会崩溃。

I just need to call CoInitialize and CoCreateInstance, and then use the instance in other functions. I can catch the uninitialized instance by checking for NULL, but I need to initialize COM - without crashing.

推荐答案

调用 CoInitialize() DllMain()是一件坏事;有很多限制你可以从 DllMain();请参阅: http://blogs.msdn.com/larryosterman/archive/2004/04 /23/118979.aspx

Calling CoInitialize() from DllMain() is a bad thing to do; there are LOTS of restrictions on what you can do from DllMain(); see here: http://blogs.msdn.com/larryosterman/archive/2004/04/23/118979.aspx

即使DID工作可靠,然后从 DllMain()是不是一个特别好的事情做COM是初始化每个线程,你不知道应用程序本身想要做什么关于COM公寓你想初始化COM的线程...这意味着你可能以一种方式初始化COM,然后应用程序可能需要以另一种方式初始化它,可能会失败,因为你的DLL已经做了...

Even if it DID work reliably then initialising COM from within DllMain() isn't an especially nice thing to do as COM is initialised per thread and you don't know what the application itself wants to do with regards to COM apartments for the thread that you want to initialise COM for... This means that you might initialise COM in one way and then the application might need to initialise it in another way and might fail because of what your DLL had done...

你可能在 DllMain()中旋转一个线程,只要你小心(参见这里http://blogs.msdn.com/oldnewthing/archive/2007/09/04/4731478.aspx ),然后在该线程上初始化COM,并做所有的COM相关工作。你需要编组任何你需要使用COM的数据,从你调用的任何线程到你自己的COM线程,并从那里进行COM调用...

You COULD spin up a thread in DllMain() as long as you are careful (see here http://blogs.msdn.com/oldnewthing/archive/2007/09/04/4731478.aspx) and then initialise COM on that thread and do all your COM related work on that thread. You would need to marshal whatever data you need to use COM with from whatever thread you're called on to your own COM thread and make the COM call from there...

然后有一个问题是,你创建的COM对象的实例(你能够可靠地做你想做的)可以从调用到你的DLL的线程中使用,以进行调用...你明白

And then there's the question of whether the instance of the COM object that you create (could you reliably do what you want to do) would be usable from the thread that was calling into your DLL to make the call... You do understand how you'd have to marshal the interface pointer if required, etc?

或者,您应该通过COM公开您的功能,然后让应用程序将您的DLL加载为COM DLL和一切都会工作很好。您可以指定您需要的公寓类型,而应用程式负责正确设定。

Alternatively you should expose YOUR functionality via COM and then have the application load your DLL as a COM DLL and everything will work just fine. You can specify the apartment type that you need and the app is responsible for setting things up for you correctly.

因此,总而言之,您不需要答案你的问题。

So, in summary, you don't need the answer to your question.

这篇关于C ++ setTimout函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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