C++ Noesis and multithreading
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:
console application throw 20x times same error:
but changed names are changed and everything works.
Here is more code:
Code: Select all
Changed(NSS(ContainerItem_NameSourceShort));
Changed(NSS(ContainerItem_NameSourceFull));
Code: Select all
ERROR: Calling thread (7048) doesn't have access to this object (4868)
Here is more code:
Code: Select all
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));
}
Re: C++ Noesis and multithreading
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
See the description of Main Thread in this document: https://www.noesisengine.com/docs/Gui.C ... orial.html
Re: C++ Noesis and multithreading
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 console throw error. But program workt ok. If I remove this code 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.
Code: Select all
Changed(NSS(ContainerItem_NameSourceShort));
Code: Select all
Changed(NSS(ContainerItem_NameSourceShort));
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.
Re: C++ Noesis and multithreading
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:
From the document:
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.IView instances are not thread-safe. All interactions must happen in the same thread that created it.
Re: C++ Noesis and multithreading
You are right, I need different way how to invoke window choose file and GUI will be still running, and I got some idea
-
sfernandez
Site Admin
- Posts: 2991
- Joined:
Re: C++ Noesis and multithreading
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.
As pointed there you cannot access objects created in one thread from another, so you would need to find an alternative.
Re: C++ Noesis and multithreading
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.
I found workaround, I am still use thread, not in noesis and it is little mess in code.
-
sfernandez
Site Admin
- Posts: 2991
- Joined:
Re: C++ Noesis and multithreading
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.
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.
Re: C++ Noesis and multithreading
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 : DWhat happens if I ignore errors?
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.
Who is online
Users browsing this forum: Bing [Bot], Google [Bot] and 26 guests