Answer the question
In order to leave comments, you need to log in
How to run a Task for a specific time?
Good evening everyone.
Due to lack of experience, I can not solve the problem. I will be grateful for help.
There is a class and a method that receives the name of the server through the socket Server.GetServerName();
Now when the server is active, the data is received quickly and well, without a glitch. But if the server slows down or is loaded or is generally disabled, then the application completely hangs, or the answer does not come, or the socket does not fall off.
I understand that you need to run it through Task.Run(() => Server.GetServerName()); And also asynchronously.
But if so, then the thread will hang until the socket falls off, or even indefinitely.
Tell me, you can somehow use Task and await / async to make sure that if Server.GetServerName () does not return a string within 30 seconds, we stop and delete the task.
I want to write a wrapper for Server.GetServerName () that will not hang up the application, and will also return an empty string or throw an exception if it could not get the data. But experience and / or brains are not enough.
Answer the question
In order to leave comments, you need to log in
string serverName = null;
var cancellationTokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(30));
Task.Run(() => serverName = Server.GetServerName(), cancellationTokenSource.Token);
public string GetServerName(CancellationToken cancellationToken)
{
// здесь непосредственно вызов по сети
cancellationToken.ThrowIfCancellationRequested();
}
public string GetServerName(CancellationToken cancellationToken)
{
// здесь непосредственно вызов по сети
if (cancellationToken.IsCancellationRequested)
{
return;
}
}
Server.GetServerName(cancellationTokenSource.Token)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question