i am trying to send a audio file(wav
) from a IoT
Device to Eventhub
. As Eventhub
has size restriction of 64Kb per message ,each message is chunked into a 7kb
byte array and sent to Eventhub
. i am trying to achieve maximum send rate [not crossing the threshold] from client.
Recording a live audio , save it in file stream and chunk it for sending. i avoided this part with custom stream implementation
public class CustomAudioStream : IRandomAccessStream{
public IAsyncOperationWithProgress<uint, uint> WriteAsync(IBuffer buffer)
{
return AsyncInfo.Run<uint, uint>((token, progress) =>
{
return Task.Run(() =>
{
using (var memoryStream = new MemoryStream())
{
using (var outputStream = memoryStream.AsOutputStream())
{
outputStream.WriteAsync(buffer).AsTask().Wait();
var byteArray = memoryStream.ToArray();
//bytes are ready to send from here
ChunkedEventSender(this, byteArray);
#if DEBUG
Debug.WriteLine("Array Length: " + byteArray.Length + " MemoryStream length:" + memoryStream.Length);
#endif
return (uint)memoryStream.Length;
}
}
});
});
}
}
But i am not able to send the byte at same speed here with REST implementation. for Sending i am using a third party wrapper so i can't do at that side.
But i can span thread to make application responsive while interacting so i use
Task.Factory.StartNew(()=>{
BackgroundSender(byte[data]);
});
i don't want to wait for Task to complete neither the outcome of Task.but when i do this most of the my request are getting "Request Timed out
" and Application is getting stuck because of those request.
Is there anyway i can make application responsive without blocking the thread.
Edit: Currently Applied Parallel.Invoke
and not loosing a single message and application is also responsive but sending drop drastically to 2-3 message/Second
Should i switch to Multi Thread model rather than using Async. Found Simlar Question like this Is async HttpClient from .Net 4.5 a bad choice for intensive load applications?