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
1
Question by benchtop · Sep 20, 2010 at 09:35 PM · udp

Asynchronous infinite loop

Hi all,

I'm using a c# script (and new to C#) to implement UDP multicast I/O based on a script that was documented in the forums. Below I've pasted the relevant code snippet. The read works great in the simulator, however, when I stop the simulator, the Async receive behavior continues. I thought perhaps I need to insert a check if the simulator is running, but is this this case? Doesn't the script stop execution when the simulator stops?

I thought this might just be an issue in the simulator so I created a build of my test project. Again, it executes just find, but when I quit the application, I get an error message that there was a shutdown error.

What do I need to do in order to appropriately stop and start this read functionality? Is there something like a 'Shutdown' function that mirrors 'Start()' where I can do any necessary cleanup?

Thanks in advance!

void Start () {

UDPSetup(); BeginAsyncReceive(); // start receiving data

}

void UDPSetup(){ listener = new UdpClient(GroupPort); listener.JoinMulticastGroup(GroupAddress); groupEP = new IPEndPoint(GroupAddress, GroupPort);

}

// Method to start an Async receive procedure private void BeginAsyncReceive() {

 ReceiveOperationInfo objInfo = new ReceiveOperationInfo(); 
 objInfo.objUDPClient = listener; 

 listener.BeginReceive(new AsyncCallback(EndAsyncReceive), objInfo);

} // Callback method from the UDPClient, when the async receive procedure received a message private void EndAsyncReceive(IAsyncResult objResult) {

 byte[] data = listener.Receive(ref groupEP); 

 // process my data


 //if(EditorApplication.isPlaying) // do I need to have a check here so it won't loop back when I don't want it to?
    BeginAsyncReceive(); 

}

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by burnumd · Sep 20, 2010 at 09:55 PM

MonoBehaviour.OnDisable () is called in a number of situations that should cover what you want to do, such as:

  • When the GameObject the script is attached to is destroyed
  • When the game quits (and the object the script is attached to gets destroyed)
  • When the script is removed (using Destroy(Component))
  • When the script is disabled

If you use this approach, you'll probably want to use OnEnable to call BeginAsyncReceive to start it up if you deactivate the script/GameObject during the course of play.

Comment
Add comment · Show 2 · 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
avatar image benchtop · Sep 20, 2010 at 10:08 PM 0
Share

Perfect - exactly what I was missing! In case it's useful for others, I've added a boolean 'shouldContinueRunning' to my script and use that as a check for whether BeginAsyncReceive() should be called again. I set this variable true when OnEnable() is called, and false when OnDisable() is called. Thanks so much!

avatar image burnumd · Sep 21, 2010 at 01:39 PM 0
Share

Glad to help. If this response answered your question, be sure to mark it as "answered" (and maybe consider upvoting). :)

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

No one has followed this question yet.

Related Questions

getting udp package info inside unity (GlovePIE) 0 Answers

UDP/TCP Simple example on how to send a receive messages from two Unity builds 2 Answers

How can I receive UDP packets into Unity Webplayer? 1 Answer

How to set up a UDP connection with a nodejs server in Unity? 1 Answer

Do I need 'pubkey' when I publish my game to Huawei App Gallery without IAP product? 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