I am running a timer for updating some of my operations, such as buffers. But I also want ping to be added.
It is added, but I think it can be improved. This is because I think it's running on the GUI thread, which is not preferred as it will freeze the GUI if something goes wrong with it. I think such a thing has occurred, and I haven't been able to locate the problem until now.
But, I am not asking for the solution for that problem, as there are other stuff. I am basically asking for ways to improve this piece of code as it doesn't seem to be optimal in any way.
private void timer1_Tick(object sender, EventArgs e)
{
try
{
if (waveProvider.BufferedDuration.Milliseconds > 40 && AudioDevices.SelectedItem.ToString() != "Wasapi Loopback")
{
waveProvider.ClearBuffer();
TimesBufferClear++;
}
currping.Text = "Current Buffer: " + waveProvider.BufferedDuration.Milliseconds.ToString() + " Clear: " + TimesBufferClear.ToString() + " Ping: " + pingSender.Send(otherPartyIP.Address).RoundtripTime.ToString() + " Buffer: " + SendStream.BufferMilliseconds;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message + "Timer");
}
}
As you can see, I have a label which contains all the things I want to update. It is fine, and I'd rather have one than many in this case.
But is this a good way to do it? It doesn't seem that good when I look at it; it's just a mess, even if it works.
Also, the PingSender
is created and disposed of outside of the thread. I suppose that is better than just "using" it every timer run.
Are there any ideas of what can be done to improve it?