Jump to content

Thread starts but does not run


Recommended Posts

Hello,

I am currently trying to develop a plugin that will allow me to connect to an external application (located on an computer) and send data back and forth. Currently I have mostly done this with sockets. I send a handshake signal to my external program written in java, it sends a handshake signal back and all is well.

But when I wanted a dedicated thread to listen to the java application i ran into some problems. The problem is on the plugin side.

I currently have this code:

Debug.Log("KSPArduinoBridge: Beginning...");
//must wait for arduino connection signal from program
var handShakeInformation = new Dictionary<string, double>();
handShakeInformation["id"] = 0;
handShakeInformation["M1"] = 1;
handShakeInformation["M2"] = 2;
handShakeInformation["M3"] = 3;
sendPacketToKSPMiddleMan(ConvertDictionaryToSend(handShakeInformation));

Debug.Log("KSPArduinoBridge: waiting");
Thread.Sleep(1000);

Debug.Log("KSPArduinoBridge: Making new thread for listening");
//MiddleManListener mml = new MiddleManListener();
Thread oThread = new Thread(ListenToMiddleMan);

Debug.Log("KSPArduinoBridge: Starting thread: ");
oThread.IsBackground = true;
oThread.Start();
            
Debug.Log("KSPArduinoBridge: past thread: ");
Thread.Sleep(5000);

I have gone through many iterations on this with different orders and ways of calling the thread. (so It may look a little messy and has lots of sleeps). The thread code is this: 

public void ListenToMiddleMan()
{
  Debug.Log("KSPArduinoBridge: thread started");
  Socket javaProgram = KSPArduinoBridge.javaprogram;

  Debug.Log("KSPArduinoBridge: java propgram grabbed");
  while (true)
  {
    if (javaProgram.Available > 0)
    {
      byte[] rcvLenBytes = new byte[4];
      javaProgram.Receive(rcvLenBytes);
      int rcvLen = System.BitConverter.ToInt32(rcvLenBytes, 0);
      byte[] rcvBytes = new byte[rcvLen];
      javaProgram.Receive(rcvBytes);
      String rcv = System.Text.Encoding.ASCII.GetString(rcvBytes);

      Debug.Log("KSPArduinoBridge: Received input: " + rcv);

      interpretInput(rcv);
    }
  }
}

It outputs all the debug logs outside of the ListenToMiddleMan thread, but never seems to enter the method itself.

Am I doing something wrong? I spent a few hours on this last night that I will never get back, so I thought I might ask for help since google wont help me either. Should I rethink even having a thread? Maybe just go with checking for items to read from the socket on update?

Source: https://github.com/FewCode/KSPArduinoBridge-KSP

 

Link to comment
Share on other sites

Debug.Log (and nearly anything in UnityEngine) won't work in a thread (it may log in the KSP_Data\output_log.txt , I have a doubt). An easy alternative is to put your log in a shared object and print them from the main thread.

Link to comment
Share on other sites

12 hours ago, sarbian said:

Debug.Log (and nearly anything in UnityEngine) won't work in a thread (it may log in the KSP_Data\output_log.txt , I have a doubt). An easy alternative is to put your log in a shared object and print them from the main thread.

Thank you very much. I didn't even think that could be the case.

Link to comment
Share on other sites

I don't KNOW if it makes what you're doing easier or harder...  but have you looked at kRPC?   It uses google protocol buffers to push data in and out.  If it does everything you need, or can be made to do what you need, it might save you some effort and help you make something that others would use too!  

All you'd need to do is implement the external java side of things, using the other language versions as an example (python, ruby, lua and C++ at the moment, I think?))  A quick search makes it look like protocol buffers has been ported to work with java already, so there's a step in the right direction!

Link to comment
Share on other sites

This thread is quite old. Please consider starting a new thread rather than reviving this one.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...