# How to notify the main game thread when the FRunnable is done?

**URL:** <https://forums.unrealengine.com/t/how-to-notify-the-main-game-thread-when-the-frunnable-is-done/438555>\
**Category:** Programming & Scripting\
**Tags:** unreal-engine\
**Created:** [November 29, 2018, 3:37pm UTC](https://forums.unrealengine.com/t/how-to-notify-the-main-game-thread-when-the-frunnable-is-done/438555 "2018-11-29T15:37:51Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![anonymous\_user\_a5e67abc](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_a5e67abc](https://forums.unrealengine.com/u/anonymous_user_a5e67abc)\
**Post date:** [November 29, 2018, 3:37pm UTC](https://forums.unrealengine.com/t/how-to-notify-the-main-game-thread-when-the-frunnable-is-done/438555/1 "2018-11-29T15:37:51Z")

</div>

Hi all !  
I am making progress with multi-threading in ue4 but I am still a bit puzzled.

**How can I notify the main thread when my computation in the FRunnable is done so that I can they query the data from the thread (with a thread safe method ofc) ?** I have read I can’t fire traditionnal events so I am a bit lost.

I could of course every tick that the thread is done running, but this is not really optimized and I not even sure this wouldn’t freeze everything since I am using a FCriticalSection lock when computing to perform a thread safe operation on FVectors (as suggested here [A new, community-hosted Unreal Engine Wiki - Announcements - Epic Developer Community Forums](https://wiki.unrealengine.com/MultiThreading_and_synchronization_Guide) ).

Any idea ?

Here is a snippet of my code :

```
uint32 ForceFieldWorker::Run()
{
	//Initial wait before starting
	FPlatformProcess::Sleep(0.03);

	//While not told to stop this thread 
	// and not yet computing the position of atoms
	while (!m_Kill)
	{
		if (m_Pause)
		{
			//FEvent->Wait(); will "sleep" the thread until it will get a signal "Trigger()"
			m_semaphore->Wait();

			if (m_Kill)
			{
				return 0;
			}
		}
		else
		{
			PC->ClientMessage("Starting Step " + FString::FromInt(counter + 1));

			//critical section :
			m_mutex.Lock();

			/** Compute one step of Force Field computation */
			forceFieldStep();

			m_mutex.Unlock();

			/* increment step counter */
			counter++;

			// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
			//prevent thread from using too many resources
			//FPlatformProcess::Sleep(0.01);
			// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

			if (IsFinished()) //if we are done, pause the thread
			{
//////////////////////////////////////////////////////////////////
NOTIFY MAIN THREAD HERE 
////////////////////////////////////////////////////////////////
				m_Pause = true;
			}
		}
	}

	return 0;
}

```

---

<div class="post-metadata">

**Author:** ![Spidermined](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Spidermined](https://forums.unrealengine.com/u/Spidermined)\
**Post date:** [November 29, 2018, 5:40pm UTC](https://forums.unrealengine.com/t/how-to-notify-the-main-game-thread-when-the-frunnable-is-done/438555/2 "2018-11-29T17:40:21Z")

</div>

I used the following website to learn about multithreading and it has an example of the thread calling an event when it is finished:

[http://deliciouscookieproduction.com/multi-threading/](http://deliciouscookieproduction.com/multi-threading/)

Summary:

- In the FRunnable subclass, the DECLARE\_EVENT macro was used to create a type FThreadFinishEvent, which was used to create a variable ResultEvent.
- When the thread’s Run method was finished, the ResultEvent.Broadcast() method was called
- In the class that uses an instance of the FRunnable subclass, ResultEvent.AddUObject() method was called, which specified a function to invoke when the event is broadcast…

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_a5e67abc](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_a5e67abc](https://forums.unrealengine.com/u/anonymous_user_a5e67abc)\
**Post date:** [January 7, 2019, 4:30pm UTC](https://forums.unrealengine.com/t/how-to-notify-the-main-game-thread-when-the-frunnable-is-done/438555/3 "2019-01-07T16:30:06Z")

</div>

Hi! Thanks for your answer.  
Sorry for the late answer, i have been quite busy lately.

Unfortunately your link is broken ☹  
I will research FThreadFinishEvent though, which should put me on the way. Thx 🙂

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_5b181ca7](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_5b181ca7](https://forums.unrealengine.com/u/anonymous_user_5b181ca7)\
**Post date:** [September 17, 2020, 8:13am UTC](https://forums.unrealengine.com/t/how-to-notify-the-main-game-thread-when-the-frunnable-is-done/438555/4 "2020-09-17T08:13:33Z")

</div>

This is wrong. Broadcast will always run bound objects within the same thread. UE4 events/delegates, as far as I can see, cannot be used for inter-thread communication.

---

<div class="post-metadata">

**Author:** ![Redex\_Davidaz](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Redex\_Davidaz](https://forums.unrealengine.com/u/Redex_Davidaz)\
**Post date:** [March 5, 2023, 1:40pm UTC](https://forums.unrealengine.com/t/how-to-notify-the-main-game-thread-when-the-frunnable-is-done/438555/5 "2023-03-05T13:40:50Z")

</div>

We can use:  
AsyncTask(ENamedThreads::GameThread, Delegate {  
Delegate.ExecuteIfBound();  
});  
It will execute the function in the GameThread.
