I have a loop:
Thread x = new Thread(() =>
{
_controller = new IpCamController("http://X.X.X.X", "user", "pass");
_controller.ImageReady += dec_FrameReady;
_controller.StartProcessing();
});
x.Start();
string ParamList;
WaveOut _waveOut = new WaveOut();
//WaveFileReader reader;
try
{
ParamList = "http://X.X.X.X/axis-cgi/audio/receive.cgi";
NetworkCredential networkCredential = new NetworkCredential("user", "pass");
WebRequest request = WebRequest.Create(ParamList);
request.Credentials = networkCredential;
HttpWebResponse response;
Stream streamResponse = null;
MemoryStream ms = new MemoryStream();
IWaveProvider reader;
BufferedWaveProvider provider;
WaveIn wi;
try
{
response = (HttpWebResponse)request.GetResponse();
streamResponse = response.GetResponseStream();
//SoundPlayer y = new SoundPlayer(ms);
}
finally
{
if (streamResponse.CanRead == true)
{
// 1048576 => 1MB
byte[] buffer = new byte[1048576];
while (true)
{
int countBytes = streamResponse.Read(buffer, 0, buffer.Length);
if (countBytes <= 0)
{
MessageBox.Show("La lectura ha sido completada.");
streamResponse.Dispose();
}
byte[] decoded = new byte[buffer.Length * 2];
//Decodifica los bytes obtenidos
ALawDecoder.ALawDecode(buffer, out decoded);
wi = new WaveIn();
wi.WaveFormat = new WaveFormat(16000, 16, 2);
provider = new BufferedWaveProvider(wi.WaveFormat);
provider.DiscardOnBufferOverflow = true;
provider.AddSamples(buffer, 0, buffer.Length);
_waveOut.Init(provider);
_waveOut.Play();
}
}
}
}
catch (Exception es)
{
MessageBox.Show(es.ToString(), "\nError Message");
}
When the loop is executed the screen does not load until the loop ends, how can I make the loop continue to execute and the other processes continue to function normally? Let me explain... The loop is executed while(true)
until it is finished, the piece of code that is above it is not executed (that is, the thread), I would like everything to be executed without having to wait for the loop to finish executing .
In conclusion: THE SCREEN REMAINS LOADING WITHOUT RUNNING THE OTHER PROCESSES.
THE PROCESS THAT MAKES IT LATE A LOT IS:
int countBytes = streamResponse.Read(buffer, 0, buffer.Length);
As an example, I dumped it to the project console, in this way you can execute asynchronously, I reordered the code a bit and used
using
it so that the memory is released as soon as you finish using it. As for youtry
catch
had two nested and in it you did thefinally
whole process if your service responded to you, but it had a lot of code and there was no security there in the finally, if something failed it broke your app.He
while
doesn't understand when you make it end and why you need it to be infinite.As for the method that takes time, you can also use that method
async
and that way I will mark it for you.In the
if
CanRead
, you hadif(streamResponse.CanRead==true)
it, which is not necessary because if the value istrue
it enters and if not, it goes on. The comparison is not necessary.