Hello, is there a way to get a reference to the ma...
# compose-desktop
l
Hello, is there a way to get a reference to the main thread (
java.lang.Thread
) of a Compose for Desktop app? My use case is checking whether some code is running on the main thread or not, to disallow running some blocking code where it could affect the UI.
d
You can check using swing utilities. I can't recall right now.
c
It’s all embedded in Swing, so the Event Dispatch Thread is the “main thread”, which you should be able to check with
SwingUtilities.isEventDispatchThread()
☝🏼 1
l
Is there a way to check that without depending on Swing?
a
Maybe you could just create a separate Thread for this blocking code?
l
It's not about finding a separate thread (
<http://Dispatchers.IO|Dispatchers.IO>
is the right dispatcher to use), it's about preventing misuse, in a world where suspending constructors don't exist. I'll check the
SwingUtilities
class to answer my own question.
a
If you really want to you can do withContext to the main thread grab the thread and after the withContext check equality
Crappy idea but it will definitely work
l
That'd be impractical since it's for synchronous code I think, but I guess I could hack something with
runBlocking(Dispatchers.Main)
, I'll have that in mind if I don't find any better.
a
Actually, just a funny question. What will happen if you are doing runblocking on the main thread to switch to the main thread, will it deadlock?
l
Either way, it'll noticeably break the app, which is desired so it doesn't reach production with the possibility of blocking the UI. I think it'd deadlock, yep, so that's a good reason to find another way.
s
to disallow running some blocking code where it could affect the UI.
Not something specific to compose, but issue like this has been solved in other frameworks using libraries like https://github.com/reactor/BlockHound (run as java agent) and it’s extensible.