Click here to Skip to main content
15,845,785 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
(Why Reply Button Not Work? I Have Edit Question and add New Information Every Time Some One Comment)First of All Sorry for my Bad English. I Don't Know a Lot English so Hope Every One Can Understand What I Mean.

Why BeginSend Have High CPU Usage. I Use Core of PSXDownloadHelper Then Create a New Application with New Design. (Every Function/Class/Method in my Program is From PSXDownloadHelper Except Design) so Why in PSXDownloadHelper CPU Usage is under 20% but in my Program at Least 50%.

PSXDownloadHelper Project: https://github.com/KOPElan/PSX-Download-Helper/tree/master/PSXDownloadHelper[^]

My PRoject: https://github.com/rasoulia/PSXDownloader/tree/main/1-%20PSXDownloader_NewVersion[^]
C#
private async void OnLocalFileSent(IAsyncResult ar)
{
    try
    {
        if (_mLocalFile!.FileStream!.Position < _mLocalFile.FileStream.Length)
        {
            byte[] buffer = new byte[1024 * 4];
            _mLocalFile.FileStream.Read(buffer, 0, buffer.Length);
           await Task.Run(() => ClientSocket?.BeginSend(buffer, 0, buffer.Length, SocketFlags.None, OnLocalFileSent, ClientSocket));// The Cause of High Cpu Usage
        }
        else
        {
            ClientSocket?.EndSend(ar);
            _mLocalFile.FileStream.Close();
        }
    }
    catch
    {
        _mLocalFile!.FileStream!.Close();
        Dispose();
    }
}


What I have tried:

The Problem is in Method OnLocalFileSent and Line
C#
ClientSocket?.BeginSend(buffer, 0, buffer.Length, SocketFlags.None, OnLocalFileSent, ClientSocket)

1. Use Socket.Send , Socket.SendAsync but Never Send Data and Only BeginSend Can Transfer Data
2. Add Task.Run to Every Line That Use BeginSend with async/await. Reduce the CPU Usage From 90% to 50%~70~

3. If I Don't Use async/await and Task.Run the CPU Usage Get up to 90% and RAM Usage up to 50%
4. Use Task.Factory.StartNew and BackGroundWorker.
5. USe Task.Delay and Thread.Sleep.
6. Change
C#
 _mLocalFile.FileStream.Read(buffer, 0, buffer.Length); //to 
int b= _mLocalFile.FileStream.Read(buffer, 0, buffer.Length);
//then use the b in buffer.Lenght in BeingSend
Posted
Updated 12-Oct-22 3:09am
v6
Comments
Dave Kreskowiak 11-Oct-22 12:32pm    
Just a heads up. NOBODY is going to go spelunking through all that code to find where the bottleneck is. You're going to have to narrow down where there code is hogging CPU and come back with a narrower question before you're going to get an answer.
Richard Deeming 12-Oct-22 4:45am    
NB: The FileStream.Read call can and will read fewer bytes than the buffer passed in. You need to store the return value, which tells you how many bytes were actually read, and pass that instead of the buffer size to the BeginSend method.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900