Your runffplay()
function has the wrong signature, so you are going to end up corrupting the thread's call stack. Read the CreateThread()
and ThreadProc
documentations.
Also, you are not doing any error handling.
Try something more like this instead:
#include <iostream>
#include <cstdlib>
#include <Windows.h>
DWORD WINAPI runffplay(LPVOID)
{
// instead of system(), consider using exec..(), or CreateProcess() directly...
const char* _cmd = "ffplay -fs -loop 0 \"D:\\dynamic wallpaper\\1.mp4\"";
int ret = std::system(_cmd);
std::cout << "system() returned " << ret << std::endl;
return 0;
}
HANDLE hThread = CreateThread(NULL, 0, runffplay, NULL, 0, NULL);
if (!hThread) {
DWORD err = GetLastError();
std::cerr << "CreateThread() failed with error " << err << std::endl;
}
else {
...
CloseHandle(hThread);
}
Otherwise, use std::thread
instead of CreateThread()
directly:
#include <iostream>
#include <thread>
#include <cstdlib>
void runffplay()
{
// instead of system(), consider using exec..(), or CreateProcess() directly...
const char* _cmd = "ffplay -fs -loop 0 \"D:\\dynamic wallpaper\\1.mp4\"";
int ret = std::system(_cmd);
std::cout << "system() returned " << ret << std::endl;
}
std::thread thrd;
try {
thrd = std::thread(runffplay);
}
catch (const std::system_error &e) {
std::cerr << "thread failed with error " << e << std::endl;
}
...
if (thrd.joinable()) {
thrd.join();
}