My component queries last.fm to retrieve scrobbles of songs. Users can select a batch of songs, right-click and last.fm will be queried, and the scrobbles will be saved to an index-data DB. This process takes time so I'm trying to move it from something that blocks foobar into a threaded_process_callback.
This appears to be working just fine, except when I try to call dispatch_refresh inside the callback, when it crashes with:
Unhandled exception at 0x74F508F2 (KernelBase.dll) in foobar2000.exe: 0xAA67913C.
My Relevant Code:
class get_lastfm_scrobbles : public threaded_process_callback {
public:
get_lastfm_scrobbles(metadb_handle_list_cref items) : m_items(items) {}
void on_init(HWND p_wnd) {}
void run(threaded_process_status & p_status, abort_callback & p_abort) {
try {
for (size_t t = 0; t < m_items.get_count(); t++) {
p_status.set_progress(t, m_items.get_size());
p_status.set_item_path(m_items[t]->get_path());
metadb_index_hash hash;
g_client->hashHandle(m_items[t], hash);
record_t record = getRecord(hash);
getLastFmPlaytimes(hash, record); // queries last.fm
setRecord(hash, record);
g_api->dispatch_refresh(guid_foo_enhanced_playcount_index, hash); // crashes here on first item
}
} catch (std::exception const & e) {
m_failMsg = e.what();
}
}
void on_done(HWND p_wnd, bool p_was_aborted) {
if (!p_was_aborted) {
if (!m_failMsg.is_empty()) {
popup_message::g_complain("Could not retrieve last.fm scrobbles", m_failMsg);
} else {
// finished succesfully
}
}
}
private:
pfc::string8 m_failMsg;
const metadb_handle_list m_items;
};
Am I missing something? I can't see where dispatch_refresh isn't allowed inside a thread. Do I need to batch these up and call them after a successful run or what?