(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[
^]
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));
}
else
{
ClientSocket?.EndSend(ar);
_mLocalFile.FileStream.Close();
}
}
catch
{
_mLocalFile!.FileStream!.Close();
Dispose();
}
}
What I have tried:
The Problem is in Method OnLocalFileSent and Line
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
_mLocalFile.FileStream.Read(buffer, 0, buffer.Length);
int b= _mLocalFile.FileStream.Read(buffer, 0, buffer.Length);