R
R
Roman Rakzin2016-10-17 22:54:02
C++ / C#
Roman Rakzin, 2016-10-17 22:54:02

How to run a cyclic (eternal) function in C# in a new thread?

There is a connection to the server via Websocket

var ws = new WebSocket("ws://localhost/client_websocket")
            { 
                ws.OnOpen += (sender, e) => this.Connected();
                ws.OnMessage += (sender, e) => Console.WriteLine(e.Data);
                ws.OnError += (sender, e) =>//делаем что-то при ошибке
                ws.OnClose += (sender, e) => //делаем что-то при отключении
                ws.Connect();
                while (true)
                {
                }
            }

I run it and because. the method spins in while , then nothing goes further in the stream.
I need to run the connection function in a new thread. I don’t understand something, how to work with Thread and Async-awayt.
Tell me how to implement this?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
@
@hatcker, 2016-10-17
@TwoRS

In the method declaration, immediately after the access modifier, type "async" and before the ws.Connect(); - await.

A
AxisPod, 2016-10-19
@AxisPod

And how do you feel sorry for the processor? There will be 100% load on the core. At least put a pause of 100ms, for example, in while. But in fact, as I understand it, all the work is already implemented inside as it should and a separate thread is not needed.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question