Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
0
Question by insominx · Jul 31, 2012 at 06:03 PM · networkingbugfreezethreadingudp

Threading with UDPClient - Editor Freezing

I'm trying to understand the relationship between Unity and Mono Threads and have found this useful: http://answers.unity3d.com/questions/180243/threading-in-unity.html ,

Now, I'm trying to use a Thread to read from a UDP socket. This works fine the first time I hit play in the editor but upon hitting play a second time, the editor freezes.

Here's a minimal script that you can use to reproduce the issue in an empty scene.

 using UnityEngine;
 using System.Collections;
 
 using System;
 using System.IO;
 using System.Text;
 using System.Net;
 using System.Net.Sockets;
 using System.Threading;
 
 public class UDP_ThreadTest: MonoBehaviour {
     
     Thread receiveThread; 
     UdpClient client; 
  
     // -------------------------------------------------------------------------
     public void Start()
     {
        init(); 
     }
  
     // -------------------------------------------------------------------------
     private void init()
     {
         receiveThread = new Thread(
             new ThreadStart(ReceiveData));
  
         receiveThread.IsBackground = true;
         receiveThread.Start();
  
         print("Start"); 
     }
                 
  // -------------------------------------------------------------------------
     private void ReceiveData() 
     {
        int somePort = 6390;
  
        try {
  
           client = new UdpClient(somePort);
           while (Thread.CurrentThread.IsAlive) 
           {
              // do stuff
           }
  
        } catch(Exception e) {
  
        }        
     } 
     
     // -------------------------------------------------------------------------
     public void OnApplicationQuit()
     {
        // end of application
        if (receiveThread != null)
        { 
           receiveThread.Abort(); 
        }
  
        print("Stop"); 
     }    
 }

Download and change ext to .cs

Just attach it to the camera, hit play, hit stop, and then hit play again. At that point the editor should freeze.

My questions: Why is this causing the editor to freeze and what's the correct thing to do here?

udp_threadtest.cs.txt (1.3 kB)
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer Wiki

Answer by tobiass · Jul 31, 2012 at 08:39 PM

Without a Thread.Sleep(x), your thread will run on highest frequency. I would check if OnApplicationQuit() is called when you run this in the editor and then stop. If the Thread doesn't end, the port you use will be used and that might block Unity somehow.

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 insominx · Jul 31, 2012 at 10:03 PM 0
Share

OnApplicationQuit is being called and I've tried using sleep with no change.

avatar image insominx · Oct 10, 2012 at 05:15 PM 0
Share

I just tried this again and it seems to work... I also put a receiveThread.Join() after the abort to be sure.

avatar image
2

Answer by arckex · Feb 25, 2014 at 07:52 PM

I solved this with only a

void OnApplicationQuit() { receiveThread.Abort(); if (client!=null) client.Close(); }

on my

Thread receiveThread; UdpClient client;

and no Thread.Sleep()

Comment
Add comment · Show 1 · 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 imbalance · Mar 24, 2016 at 12:52 AM 0
Share

thinks! it's really fixed !

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

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Need help on LAN server discovery via UDP Broadcast 0 Answers

UDP using HTC VIVE tracker 0 Answers

EndOfStreamException: Failed to read past end of stream. 0 Answers

How to avoid freezing UI in Tcp Socket Connection? 0 Answers

UDP implementation in Unity, unable to send to two different machines 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