Using a thread to communicate with components which are created after the point the thread starts running

gui

#1

So the way I have attempted to handle this problem is to create a function which looks like this:

void InitLoadSamples::addComponentReferences(Component& preferencesButton,
ErrorWindow& errorWindow)
{
    curPreferencesButton = &preferencesButton;
    curErrorWindow = &errorWindow;
}

Defined privately like:

 Component* curPreferencesButton{nullptr};
 ErrorWindow* curErrorWindow{nullptr};

and accessed in my run() code as follows:

if (allFiles.hashCode64() == directoryHash)
{
	curSynth->sampleSetup(curSamplePath, this);
	if (threadShouldExit())
	{
		return;
	}
	if (curPreferencesButton)
	{
		MessageManager::callAsync([this]()
		{
			curPreferencesButton->removeChildComponent(1);
		});
	}
}
else
{
	if (curPreferencesButton)
	{
		MessageManager::callAsync([this]()
		{
			curPreferencesButton->removeChildComponent(1);
			curPreferencesButton->addAndMakeVisible(curErrorWindow);
		});
	}
}

This solution is clearly incorrect, since my plugin will crash and tell me I have a write access error as soon as any external code which has access to thread tries to run addComponentReferences()

So I was hoping to have a discussion around best practices for accessing components in a thread which are created after the thread has already started running!