Hello !
Context
I currently working on internet call to fetch a XML page that is launched only one time, during the plugin initialisation.
I would have some thread related question and be sure to use the best practices.
I didn’t find it on the forum so I don’t know if I missused the search feature or if it hasn’t been discussed, so it could maybe help other people.
Let’s say I’ve the following URL
auto url = URL("https://mywebsite/fileToFetch.xml");
I know that to fetch this file I’ve to do
url.readEntireXmlStream();
Even it it’s a simple call, I’m used to do that in specific thread in order to avoid any problem in the message thread. The result of the the fetch will be then send to the message thread using juce::MessageManager::callAsync
Simple static function solution
I’ve read that using a simple
juce::Thread::launch
Could cause some issue if the pluging is shutdown.
Custom class and juce::Thread inheritance solution
So I tried to create a custome class that inherit from juce::Thread and I implemented the run function like this:
void MyCustomClass::run() {
if(threadShouldExit()) { return; }
auto result = fetchXMLFile();
auto weakRef = juce::WeakReference<MyCustomClass>(this);
juce::MessageManager::callAsync([weakRef, result] {
if (weakRef == nullptr) { return; }
if (weakRef->callbackToSendResult) {
weakRef->callbackToSendResult(result);
}
});
}
In the AudioPluginHost app (only for the moment), I encountered an issue where the thread was forcedly killed.
I tried to use stopThread(100); in my custom class destructor. Nothing changed.
I also tried to stop the thread after the xml sent via callback. Nothing changed too.
juce::ThreadPool solution
I also see that a good thing was to use juce::ThreadPool even if it seemed to be a little massive regarding the simple call I want to make, if it would be safe it would be perfect.
However, when I use it, once again I cannot close the thread and I see during the whole usage of my app the thread I created.
Questions
So, what would be the best practice to make a simple internet call to fetch a XML file ?
It seem to be a naive question but I’m really worried about this opened thread. I’ve the feeling I’m missing something obvious.
Feel free to forward any forum threads or documentations that I could have missed.
Thanks
