Execute loop without freezing game

So I’m on UE 4.8.3 source code with Steam SDK 1.36 fully integrated.

I have this particular c++ function that is BlueprintCallable to unsubscribe from a steam workshop item. Using the SteamAPI, this function creates a steam handler for the request and registers a CallBack function.

The c++ function then waits for the callback to finish before continuing on. Here’s the wait code:

while (pItem->m_SteamCRUnsubscribe.IsActive())
	{
		Sleep(100);
		SteamAPI_RunCallbacks();
		i++;
		if (i > 50) // fail after 50 loops (5 seconds)
		{
			break;
		}
	}

My problem, is that when I do this in a blueprint ForEach loop, like unsubscribing from an array list of Workshop IDs; the game interface stops refreshing (My circular throbber freezes as well as all game interaction) until the loop has finished.

I’m fairly sure the c++ function is holding up the game thread. Even though the c++ code is only responsible for unsubscribing one item at a time, the BP is responsible for the loop and in between refreshes. So I need a way to call the c++ function in a thread safe manner, I just don’t know how.

I was hoping someone has run in this the before and its just a rookie coding mistake.

I think I know the direction I need to take, although I wanted to avoid it… Multi-threading is the solution here.
Thanks to 's numerous how-to posts, I should be able to execute what I need based off of the PrimeNumber example he gave.

I’ll mark this as the answer if I can confirm.

You could set a Timer and execute the function every x time from Blueprint and get rid of the while loop in the function.

You are absolutely correct, have a timer check for me, and execute the necessary steps from there… What happens, is when this whole routine starts, the game app dims, user input is shut off to key areas, and a circular throbber is displayed.

All of those steps are done by a single function call to the parent widget, and terminated by a similar function call. I can have the timer execute the terminate wait call!

I have to setup a timer anyway with mult-threading so… Brilliant!

I will still go with the multi-threading, just to get my head in to it. It may be overkill for this simple task, but I will need to know how to do it for later projects.