Game in Unity play mode doesn't work but works in build

Hello! I have a strange problem with unity that i can’t find any clue about the source of that. when i use some mixture of TAP (Async) and EAP and multi-threading, i run into a problem: unity player doesn’t work but build works well!
I don’t have a exact sample code, and i’m not a professional at multi-threading and concurrent programming. but i know one thing: my code is correct and woks well in a C# application and also in PC or Android build. but it doesn’t work in unity player. i guess it stops when first event get called form another thread.

in this thread i like to discuss about restrictions of using Asynchronous Programming Patterns in unity and also the unity player limitations over build.

i must mention that i use https://assetstore.unity.com/packages/tools/integration/log-viewer-12047 in my build but i guess in the first callback event from another thread, debugger doesn’t log anything. and i completely get blind and i can’t detect exceptions and error or any log!

If you can’t show the specific code, I’d recommend creating a simplified version of that code. If you’re saying that doing a certain multi-threaded operation causes the game not to work in Play Mode, then perhaps you can create a really simple version of that code in a new project to demonstrate the behavior. Maybe just spin up a new thread, and have it do nothing, if that’s enough to reproduce the issue. From that point, you could either post the code, or report a bug if it’s reliably broken.

this is the shortest code that i can provide to show the problem: in unity just create this script and reference a UI text

using System.Net;
using System.Net.Sockets;
using UnityEngine;
using UnityEngine.UI;

public class NewBehaviourScript : MonoBehaviour
{
    public Text text;
    Socket socket;

    private void Start()
    {
        socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        socket.Connect(IPAddress.Parse("127.0.0.1"), 11111);
        SocketAsyncEventArgs socketAsyncEventArgs = new SocketAsyncEventArgs();
        socketAsyncEventArgs.Completed += SocketAsyncEventArgs_Completed;
        socketAsyncEventArgs.SetBuffer(new byte[4], 0, 4);
        socket.ReceiveAsync(socketAsyncEventArgs);
    }

    private void SocketAsyncEventArgs_Completed(object sender, SocketAsyncEventArgs e)
    {
        print(e.BytesTransferred);
        text.text = e.Buffer[0].ToString();
        socket.ReceiveAsync(e);
    }
}

then create a windows form app like this:

using System;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Timers;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        Socket socket;
        byte counter;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_LoadAsync(object sender, EventArgs e)
        {
            Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            listener.Bind(new IPEndPoint(IPAddress.Any, 11111));
            listener.Listen(1);
            socket = listener.Accept();

            System.Timers.Timer timer = new System.Timers.Timer(1000d);
            timer.Elapsed += Timer_Elapsed;
            timer.Start();
        }

        private void Timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            socket.Send(new byte[] { counter });
            this.Invoke(new Action(() => { listBox1.Items.Add(counter.ToString() + " sent"); }));         
            counter++;
        }
    }
}

this code doesn’t work in unity player for me and UI text never get changed but in build it works like a charm!!!

I posted shortest possible code! i’m using unity 2019.3.3 in windows 7.

Well, you can try submitting a bug report with your repro project. I personally would never try to use Windows Forms within a Unity project, and I have no idea whether that’s at all viable. Do you have some particular requirement to use Windows Forms here? That seems very strange to me.

Anyway, I don’t have much advice to offer here, other than finding a different way to do what you’re trying to do that’s more along the lines of how things are done in Unity.