locked
How can I just show cancel information when my task is cancelled? RRS feed

  • Question

  • User-978659149 posted

    This is my code :

    private static void SendLoop()
    {
        CancellationTokenSource source = new CancellationTokenSource();
    
        while (true)
        {
            Console.WriteLine("Enter a request: ");
            string req = Console.ReadLine();
    
            byte[] buffer = Encoding.ASCII.GetBytes(req);
            _clientSocket.Send(buffer,0,buffer.Length,SocketFlags.None);
    
            if (req != string.Empty)
            {
                var task = Task.Run(() =>
                {
                    if(source.IsCancellationRequested != true)
                    {
                        var receivedBuffer = new byte[2048];
                        int rec = _clientSocket.Receive(receivedBuffer, SocketFlags.None);
    
                        byte[] data = new byte[rec];
                        Array.Copy(receivedBuffer, data, rec);
                        Console.WriteLine("Received : " + Encoding.ASCII.GetString(data));
                    }
                    else
                    {
                        source.Dispose();
                    }
                },source.Token);
    
    
                    if (task.Wait(TimeSpan.FromSeconds(10)))
                    {
                        Console.WriteLine("Task end");
                    }
                    else
                    {
                        Console.WriteLine("task cancel" + task.Status);
                        source.Cancel();
                        Console.WriteLine(source.IsCancellationRequested);
                        _clientSocket.Close();
                    }
            }
        }
    }

    I want to modify my code to just have the cancel information and not a message if the task is completed because finally I will know that my task is completed if I receive the data from the server.

    Do you have any idea how can I modify my code ?

    Tuesday, January 21, 2020 9:02 AM

Answers

  • User303363814 posted
    if (!task.Wait(TimeSpan.FromSeconds(10)))
    {
     Console.WriteLine("task cancel" + task.Status);
     source.Cancel();
     Console.WriteLine(source.IsCancellationRequested);
     _clientSocket.Close(); 
    }

    Alternatitevly, you could delete the Console.WriteLine statement. Exactly the same

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 22, 2020 8:47 PM

All replies

  • User303363814 posted

    Can you explain a little more?

    As you have written the question the answer is just to delete the console.writeline statements that you do not want.  I can't believe that is all their is to your question!

    Tuesday, January 21, 2020 10:31 PM
  • User288213138 posted

    Hi soleyne,

    I want to modify my code to just have the cancel information and not a message if the task is completed because finally I will know that my task is completed if I receive the data from the server.

    According to your description, I couldn’t understand your requirement clearly.

    What is cancel information and message?

    else
                    {
                        source.Dispose();
                    }

    This is the code for task is cancelled, if you want to show cancel information, you can do something in it.

    Best regards,

    Sam

    Wednesday, January 22, 2020 2:18 AM
  • User-978659149 posted

    I want to modify my code to don't have this part.

    if (task.Wait(TimeSpan.FromSeconds(10)))
    {
        Console.WriteLine("Task end");
    }

    Because if the task is not cancel we will already show the result thinks to this.

    if(source.IsCancellationRequested != true)
    {
        var receivedBuffer = new byte[2048];
        int rec = _clientSocket.Receive(receivedBuffer, SocketFlags.None);
    
        byte[] data = new byte[rec];
        Array.Copy(receivedBuffer, data, rec);
        Console.WriteLine("Received : " + Encoding.ASCII.GetString(data));
    }

    Wednesday, January 22, 2020 8:49 AM
  • User-978659149 posted

    I want to modify my code to don't have this part.

    if (task.Wait(TimeSpan.FromSeconds(10)))
    {
        Console.WriteLine("Task end");
    }

    Because if the task is not cancel we will already show the result thinks to this.

    if(source.IsCancellationRequested != true)
    {
        var receivedBuffer = new byte[2048];
        int rec = _clientSocket.Receive(receivedBuffer, SocketFlags.None);
    
        byte[] data = new byte[rec];
        Array.Copy(receivedBuffer, data, rec);
        Console.WriteLine("Received : " + Encoding.ASCII.GetString(data));
    }
    Wednesday, January 22, 2020 8:49 AM
  • User303363814 posted
    if (!task.Wait(TimeSpan.FromSeconds(10)))
    {
     Console.WriteLine("task cancel" + task.Status);
     source.Cancel();
     Console.WriteLine(source.IsCancellationRequested);
     _clientSocket.Close(); 
    }

    Alternatitevly, you could delete the Console.WriteLine statement. Exactly the same

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 22, 2020 8:47 PM