Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by JoeStrout · May 01, 2011 at 03:39 AM · stopasyncsocketthread

Socket continues to operate even after stopping the run

I'm experimenting with using C# sockets directly. They work fine, except for one little problem: they don't break the connection, and in fact continue to work and execute code, even after I've stopped the run in the Unity IDE.

I suspect this is because when you use asynchronous sockets in C#, it automatically creates another thread on which to receive the data. I understand that you need to be careful with these in Unity, catch all exceptions, and don't call into the Unity framework from such a thread. That's all doable, but this part of the app continuing to run even after I've clicked the Stop button is really harshing my buzz.

Anybody know a way to (1) get the Unity IDE to stop all threads (and/or sockets) when you stop the run, short of quitting the whole IDE? Or, (2) get some sort of callback in code when the user stops the run, so I can close the socket myself?

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Santosh Patil · Aug 20, 2013 at 11:26 AM 0
Share
 void OnDisable()
 {
         if ( receiveThread)
     receiveThread.Abort();
     client.Close();
 } 

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Ejlersen · Aug 20, 2013 at 08:38 PM

I don't know if this is the perfect answer, but here is a couple of things I use to do, when working with threads.

  • Add a bool to my threads loop.
    • When I abort my thread, I set the bool to false and check if its aborted properly.

For the socket. Remember to close the stream: client.GetStream().Close(); Else try and call client.Client.Close();

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

How to change UI or GameObject with Thread 0 Answers

Task.Wait seems to block Unity forever, Task variable is returned from an Async function 0 Answers

C# Socket in Unity (in ClientSocket : MonoBehaviour) does not receive server package(s) (keeps losing). 1 Answer

ThreadCheck error in socket programming and instantiate a object!!! 1 Answer

Client TCP - State of the art 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges