Find out if I'm on the unity thread

Boris Callens

How can I check if the thread I'm on is the Unity thread?

I tried capturing the threadId at constructor time, but somewhere along the lifetime of the program, the threadId moves.

In my project, some secondary thread processes need access to a newly created object.
I use the producer-consumer pattern so they can be created on the Unity thread. An object factory queues a request and on Update() the objects I requested are instantiated on the correct thread. Between Queued and Instantiated the factory method waits for an ObjectCreated event with an AutoResetEvent.

Now sometimes this factory will be called from the main thread and the AutoResetEvent will block its own thread. I also tried it the dirty way with

// First try on this thread
try
{
    return action();
}
catch (ArgumentException ex)
{
    Debug.Log("Tried on same thread, but failed. "+ex.Message);
}
PushToQueueAndWait(action);

But when unity throws the exception, caught or not, the program halts.

If I could check whether I'm on the correct thread, I could switch between queueing and just executing.

Boris Callens

I solved it by capturing the entire thread and then equalling it like so:

public void Start(){
    mainThread = System.Threading.Thread.CurrentThread;
}

bool isMainThread(){
    return mainThread.Equals(System.Threading.Thread.CurrentThread);
}

Related: http://answers.unity3d.com/questions/62631/i-wonder-about-thread-id-plz-answer-me.html

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

How do I find out the numeric ID of the current Postgres Transaction?

来自分类Dev

Can I find out if a given ext4 block is in the inode table, and if so, can I pick it out of a journal with no header by hand?

来自分类Dev

How can I find out what version is passed to setup in setup.py?

来自分类Dev

How can I find out how much physical space a record (BLOB) in a sqlite database uses?

来自分类Dev

How can I find out if an data- attribute is set to an empty value?

来自分类Dev

How do I find out which process is using so much of my RAM?

来自分类Dev

Find out the list of tests that are skipped

来自分类Dev

Find out glibc compilation options

来自分类Dev

How to find out Mule flow status

来自分类Dev

Is there any way to find out the tables relationship?

来自分类Dev

How to find out what the source of an NS Log is?

来自分类Dev

从 i3 wm 更改为 Unity

来自分类Dev

了解Unity的GameObject.Find(),GetComponent()和对象回收

来自分类Dev

find out the domain from an emailId through regex in java

来自分类Dev

How to find out the implementation details for a certain standard C++ function?

来自分类Dev

Find out what device /dev/root represents in Linux?

来自分类Dev

Unity中的Gnome3 Win + M(Ubuntu 12.04 LTS)

来自分类Dev

Unity中的Gnome3 Win + M(Ubuntu 12.04 LTS)

来自分类Dev

Unity : UI Elements out of Camera ..(里面的所有图像和代码)

来自分类Dev

I'm new to maven and jenkins

来自分类Dev

Should I use asynchronous methods within a background thread?

来自分类Dev

How do I gracefully handle this exception in a background thread in a Windows service?

来自分类Dev

Why is my rails insert slow even if I use thread?

来自分类Dev

Atomic Operation Thread Safety - Do I Need a "Mirror" Atomic Read?

来自分类Dev

How do I assign contents of a string to a text field in Unity 4.6?

来自分类Dev

Unity ArgumentOutOfRangeException:参数超出范围。参数名称:i

来自分类Dev

what happens to background processes stdout and stderr when I log out?

来自分类Dev

How do I proc out with tilde expansion AND $PATH searching in Haskell?

来自分类Dev

Windows 7 come out of sleep when I open remote desktop

Related 相关文章

  1. 1

    How do I find out the numeric ID of the current Postgres Transaction?

  2. 2

    Can I find out if a given ext4 block is in the inode table, and if so, can I pick it out of a journal with no header by hand?

  3. 3

    How can I find out what version is passed to setup in setup.py?

  4. 4

    How can I find out how much physical space a record (BLOB) in a sqlite database uses?

  5. 5

    How can I find out if an data- attribute is set to an empty value?

  6. 6

    How do I find out which process is using so much of my RAM?

  7. 7

    Find out the list of tests that are skipped

  8. 8

    Find out glibc compilation options

  9. 9

    How to find out Mule flow status

  10. 10

    Is there any way to find out the tables relationship?

  11. 11

    How to find out what the source of an NS Log is?

  12. 12

    从 i3 wm 更改为 Unity

  13. 13

    了解Unity的GameObject.Find(),GetComponent()和对象回收

  14. 14

    find out the domain from an emailId through regex in java

  15. 15

    How to find out the implementation details for a certain standard C++ function?

  16. 16

    Find out what device /dev/root represents in Linux?

  17. 17

    Unity中的Gnome3 Win + M(Ubuntu 12.04 LTS)

  18. 18

    Unity中的Gnome3 Win + M(Ubuntu 12.04 LTS)

  19. 19

    Unity : UI Elements out of Camera ..(里面的所有图像和代码)

  20. 20

    I'm new to maven and jenkins

  21. 21

    Should I use asynchronous methods within a background thread?

  22. 22

    How do I gracefully handle this exception in a background thread in a Windows service?

  23. 23

    Why is my rails insert slow even if I use thread?

  24. 24

    Atomic Operation Thread Safety - Do I Need a "Mirror" Atomic Read?

  25. 25

    How do I assign contents of a string to a text field in Unity 4.6?

  26. 26

    Unity ArgumentOutOfRangeException:参数超出范围。参数名称:i

  27. 27

    what happens to background processes stdout and stderr when I log out?

  28. 28

    How do I proc out with tilde expansion AND $PATH searching in Haskell?

  29. 29

    Windows 7 come out of sleep when I open remote desktop

热门标签

归档