本文介绍了QNetworkAccessManager->在DLL调用时获得冻结的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我想要做一些网络的一个DLL,这个DLL文件从C#应用程序作为非托管的DLL调用。
所有的初始化工作正常,但它每次冻结 - > get()方法应该运行。我有这样的代码:

I have a dll where I want to do some networking, this dll is called from an C# application as an unmanaged DLL.All initialization works fine but it freezes everytime the ->get() is supposed to run. I have this code:

.........
QUrl path(remotePath);
QNetworkRequest request(path);

currentFile.setFileName(localPath);
if(!currentFile.open(QIODevice::WriteOnly)){
doCallback("failed to open: " + localPath);
}
doCallback("before get: " + remotePath);
QNetworkReply* reply = this->manager->get(request);
doCallback("after get: " + localPath);
...........



之前得到回调执行罚款,但从来没有一后获得所以我想,当管理者试图将get()方法使其完全冻结。有我错过了什么,或通过DLL这只是简单的不可能?

The "before get" callback is executed fine but never the one "after get" so I guess it completely freezes when manager is trying the Get() method. Have I missed something or is this just plain impossible through an DLL?

推荐答案

欧凯所以我得到这个工作,我想我会分享我的知识。用我的非托管的DLL我出口创造了新的QCoreApplication,通过这个我可以用QCoreApplication对象作为父母我的类,它执行所有联网后使用该事件循环的初始化函数。

Okey so I got this working and I thought I would share my knowledge. With my unmanaged dll I exported an init function which creates a new QCoreApplication, through this I can later use the event loop by using the QCoreApplication object as parent to my class which executes all networking.

QCoreApplication* coreApp = 0;
extern "C" __declspec(dllexport) void initdll()
{
    if (!coreApp) {
        int argc    = 0;
        coreApp = new QCoreApplication(argc, 0);
    }
    if(!MyFunc){
        MyFunc = new QtDll(coreApp);
    }
}

这是在我的C#调用这个初始化函数(或任何)应用程序之前,我想我明白了,在我的第一篇文章中的代码加入事件循环的一个exec工作的任何网络。

By calling this init function in my C# (or whatever) application before I tried any networking I got it working with the code in my first post by adding an exec of the event loop.

QNetworkReply* reply = this->manager->get(request);
currentReply = reply;
QEventLoop eLoop;
connect(reply, SIGNAL(readyRead()), this, SLOT(slotReadyRead()));
eLoop.exec( QEventLoop::ExcludeUserInputEvents );



彼得感谢您的反馈意见。

Thanks Peter for your feedback.

这篇关于QNetworkAccessManager->在DLL调用时获得冻结的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 12:45