Wanderer
Topic Author
Posts: 168
Joined: 08 May 2017, 18:36

C++ Noesis and multithreading

20 Aug 2017, 20:35

I created new thread for choose file window. The window is classic file window explorer from Windows OS. Everything is fine and code works, but when this is called:
Changed(NSS(ContainerItem_NameSourceShort));
Changed(NSS(ContainerItem_NameSourceFull));
console application throw 20x times same error:
ERROR: Calling thread (7048) doesn't have access to this object (4868)
but changed names are changed and everything works.
Here is more code:
void ContainerItem::m_func_button_SelelctSource(Noesis::BaseComponent * sender, const Noesis::RoutedEventArgs & args)
{
	if (m_thread_FileOpenClose.joinable())
	{
		m_thread_FileOpenClose.join();
	}
	m_thread_FileOpenClose = std::thread(
		&ContainerItem::m_func_SelectSourceFile, this
	);
}
void ContainerItem::m_func_SelectSourceFile()
{
	m_ContainerDataItemDelegat->m_func_FileOpenSourceFile();
	m_func_SetNameSourceFull(MyDelegate::wstrToUTF8(m_ContainerDataItemDelegat->GetSourcePath()).c_str());
	m_func_SetNameSource(MyDelegate::wstrToUTF8(m_ContainerDataItemDelegat->GetSourceFileName()).c_str());
}
void ContainerItem::m_func_SetNameSource(const NsChar * text)
{
	m_NsString_SourceMain = text;
	Changed(NSS(ContainerItem_NameSourceShort));
}
 
nikobarli
Posts: 180
Joined: 26 Apr 2017, 06:23

Re: C++ Noesis and multithreading

21 Aug 2017, 06:43

You need to access IView and its objects from the same thread you created them.

See the description of Main Thread in this document: https://www.noesisengine.com/docs/Gui.C ... orial.html
 
Wanderer
Topic Author
Posts: 168
Joined: 08 May 2017, 18:36

Re: C++ Noesis and multithreading

21 Aug 2017, 17:25

I want to be sure if we understand each other, I am not rendering any Noesis GUI in second thread. What I am doing in second thread is invoke Windows Explorer and after choose file, it update GUI only with calling method. And if it reach
Changed(NSS(ContainerItem_NameSourceShort));
console throw error. But program workt ok. If I remove this code
Changed(NSS(ContainerItem_NameSourceShort));
no problems, but then text is not changed.

I use one window and one IView, but more threads. I checked description, if I understand, I need for each thread new IVIew? But for what if I use only one IView and don't need another? But maybe my concept is bad.
 
nikobarli
Posts: 180
Joined: 26 Apr 2017, 06:23

Re: C++ Noesis and multithreading

21 Aug 2017, 18:23

Assuming that you created the IView inside the GUI thread, then you also need to call Changed(...) inside the same GUI thread, because Changed(...) is modifying the state of IView.

From the document:
IView instances are not thread-safe. All interactions must happen in the same thread that created it.
Is there any reason you spawn a new thread for the file window, rather than just doing it in the same GUI thread ? That complicates thing a lot, as you have already experienced.
 
Wanderer
Topic Author
Posts: 168
Joined: 08 May 2017, 18:36

Re: C++ Noesis and multithreading

21 Aug 2017, 18:44

You are right, I need different way how to invoke window choose file and GUI will be still running, and I got some idea :)
 
User avatar
sfernandez
Site Admin
Posts: 2991
Joined: 22 Dec 2011, 19:20

Re: C++ Noesis and multithreading

23 Aug 2017, 20:46

Thanks @nikobarli for sharing the corresponding documentation.

As pointed there you cannot access objects created in one thread from another, so you would need to find an alternative.
 
Wanderer
Topic Author
Posts: 168
Joined: 08 May 2017, 18:36

Re: C++ Noesis and multithreading

23 Aug 2017, 22:23

Ok, but what If I don't render in second thread but only send messages? It is still problem? What happens if I ignore errors? Program runs without crash, only this errors appear and in final program there will be no console, then no errors appears.

I found workaround, I am still use thread, not in noesis and it is little mess in code.
 
User avatar
sfernandez
Site Admin
Posts: 2991
Joined: 22 Dec 2011, 19:20

Re: C++ Noesis and multithreading

24 Aug 2017, 11:41

Render is not the problem here, as all the rendering data is correctly isolated from view update data.

If you are not getting crashes or having problems now when you access the View or UI elements from a different thread doesn't mean that it won't crash later or when executing in other computers.
 
User avatar
jsantos
Site Admin
Posts: 3918
Joined: 20 Jan 2012, 17:18
Contact:

Re: C++ Noesis and multithreading

24 Aug 2017, 20:13

What happens if I ignore errors?
The fact that you are getting an error (non fatal one, so continuable) but your program works correctly only means that you are being lucky : D
If you are really sure about this being safe (and the only way to be sure would be using mutexes... you probably dont want to follow this path) you could ignore the error inside the error handler.
 
Wanderer
Topic Author
Posts: 168
Joined: 08 May 2017, 18:36

Re: C++ Noesis and multithreading

25 Aug 2017, 17:35

thanks all for explanations.

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 26 guests