Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 rag29 · Jul 24, 2015 at 06:44 PM · c#unity 5threadsmultithreading

Unity Threading Issue: Not able to delegate task

I am using a raspberry pi to send information to Unity via UDP. In order to circumvent the multi-threading issue with Unity, I am delegating a task in a part of the script. I am getting an error stating:

"error CS0120: An object reference is required to access non-static member `UDPReceive.te'"

How can I properly send the information using this task delegation technique? I have the raspberry pi sending information properly to Unity but am unable to retrieve the information due to the threading issue, which is what I am trying to work around here.

 using UnityEngine;
 using System.Collections;
 
 using System;
 using System.Text;
 using System.Net;
 using System.Net.Sockets;
 using System.Threading;
 
 public class UDPReceive : MonoBehaviour {
 
     // receiving Thread
     Thread receiveThread;
     
     // udpclient object
     UdpClient client;
     
     // public
     public string IP = "128.235.117.210";
     public int port; // define > init
     public TaskExecutorScript te;
 
 
     // NEW
     public int X;
     public int Y;
     string[] split_data;
     public static GameObject cube;
     
 
 
     // infos
     public string lastReceivedUDPPacket="";
     
     private string info;
     
     // start from shell
     private static void Main()
         //public void Update()
     {
         
         
         UDPReceive receiveObj=new UDPReceive();
         receiveObj.init();
         
         string text="";
         do
         {
             text = Console.ReadLine();
 
         }
         while(!text.Equals("exit"));
 
 
         
         // NEW
         te.ScheduleTask(new Task(delegate
                                                  {
             // Add whatever Unity API code you want here
             //split_data = text.Split(' ');
             //X = Int32.Parse(split_data [0]);
             //Y = Int32.Parse(split_data [1]);
 
             transform.position = new Vector3(0, 0, 0);
         }
         ));
         
         
         
         
         
 
         
         
     }
     // start from unity3d
     public void Start()
     {
         te = GetComponent<TaskExecutorScript> ();
         init();  
         
         // cube = GameObject.Find ("Cube");
     }
     
     // OnGUI
     void OnGUI()
     {
         Rect rectObj=new Rect(40,10,200,400);
         GUIStyle style = new GUIStyle();
         style.alignment = TextAnchor.UpperLeft;
         GUI.Box(rectObj,"# UDPReceive\n"+IP+": "+port+" #\n"
                 + "shell> nc -u "+IP+": "+port+" \n"
                 + "\nLast Packet: \n"+ lastReceivedUDPPacket
                 ,style);
     }
     
     // init
     private void init()
     {
         // Endpunkt definieren, von dem die Nachrichten gesendet werden.
         print("UDPSend.init()");
         
         // define port
         port = 6666;  
         
         // status
         print("Sending to "+IP+": "+port);
         print("Test-Sending to this Port: nc -u "+IP+": "+port+"");
         
         
         // ----------------------------
         // Abhören
         // ----------------------------
         // Lokalen Endpunkt definieren (wo Nachrichten empfangen werden).
         // Einen neuen Thread für den Empfang eingehender Nachrichten erstellen.
         receiveThread = new Thread(
             new ThreadStart(ReceiveData));
         receiveThread.IsBackground = true;
         receiveThread.Start();
         
     }
     
     // receive thread
     private  void ReceiveData()
     {
         
         client = new UdpClient(port);
         while (true)
         {
             
             try
             {
                 // Bytes empfangen.
                 IPEndPoint anyIP = new IPEndPoint(IPAddress.Any, port);
                 byte[] data = client.Receive(ref anyIP);
                 
                 // Bytes mit der UTF8-Kodierung in das Textformat kodieren.
                 string text = Encoding.UTF8.GetString(data);
                 
                 // Den abgerufenen Text anzeigen.
                 print(">> " + text);
 
                 // latest UDPpacket
                 lastReceivedUDPPacket=text;
                 
             }
             catch (Exception err)
             {
                 print(err.ToString());
             }
         }
     }
     
     // getLatestUDPPacket
     // cleans up the rest
     public string getLatestUDPPacket()
     {
         return lastReceivedUDPPacket;
     }
     /*
         public void UseInfo()
         {
             info = lastReceivedUDPPacket;
             arraySplit = info.Split ('0');
             x =  int.Parse(arraySplit [0]);
             y =  int.Parse(arraySplit [1]);
             this.transform.position = new Vector3 (x, y, 0);
 
         } */
     
     void OnDisable() 
     { 
         if ( receiveThread!= null) 
             receiveThread.Abort(); 
         
         client.Close(); 
     } 
 }

and

 using UnityEngine;
     using System;
     using System.Collections;
     using System.Collections.Generic;
 
     public delegate void Task();
 
     public class TaskExecutorScript : MonoBehaviour {
 
         private Queue<Task> TaskQueue = new Queue<Task>();
         private object _queueLock = new object();
 
         // Update is called once per frame
         void Update () {
             lock (_queueLock)
             {
                 if (TaskQueue.Count > 0)
                     TaskQueue.Dequeue()();
             }
         }
 
         public void ScheduleTask(Task newTask)
         {
             lock (_queueLock)
             {
                 if (TaskQueue.Count < 100)
                     TaskQueue.Enqueue(newTask);
             }
         }
     }

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

0 Replies

· Add your reply
  • Sort: 

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

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How to use multi-threading on Hololens 1 Answer

Update Method doesn't see value set by other thread 0 Answers

Google Daydream Thread with Unity 1 Answer


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