As Daniel suggests, if you need guaranteed 40ms intervals, you need
something other than managed code (or a _very_ carefully crafted CF
thread to do it).
Something other... what do you mean, native code? Because, even if I
create a buffer of 160ms (with CF) of data (Timer every 200 ms) I see a
random delay (probably OS):
The code
public class dataState
{
public const int BUFFSIZE=3204;
public int CurrPosition;
public byte counter=0;
public byte[] Buff;
public byte[] buffSend=new byte[BUFFSIZE];
public bool Done=false;
public bool LedOn=true;
public MemoryMan mm=new MemoryMan();
public dataState( byte[] Buff)
{
this.Buff=Buff;
CurrPosition=4;
fillbuffSend();
}
public void fillbuffSend()
{
this.buffSend[0]=counter;
if (counter>=255)
counter=0;
else{
counter++;
}
if ( Buff.Length-CurrPosition>=BUFFSIZE)
{
Array.Copy( Buff,CurrPosition,buffSend,4,buffSend.Length-4);
CurrPosition+=BUFFSIZE;
}
else
{
Array.Copy(Buff,CurrPosition,buffSend,4,Buff.Length-CurrPosition-4);
CurrPosition+=BUFFSIZE;
Done=true;
}
}
}
/// <summary>
/// Descrizione di riepilogo per Class1.
/// </summary>
public class SoundSocket
{
#region "fields"
//private Socket _sock;
private IPEndPoint _TargetIP;
private int _UDPPort=(int)0xff00;
private Socket _sock=null;
//private NetworkStream _soundStream;
private WaveInfo wi=null;
private UdpClient listener = new UdpClient();
private IPEndPoint groupEP;
private System.Threading.Timer t;
public long tic=0;
public long tac=0;
public bool ThreadAlive=true;
#endregion
#region "properties"
/// <summary>
///
/// </summary>
public IPEndPoint TargetIP
{
get{
return _TargetIP;
}
set{_TargetIP=value;}
}
/// <summary>
///
/// </summary>
public int UDPPort
{
get
{
return _UDPPort;
}
set{_UDPPort=value;}
}
#endregion
public SoundSocket(IPAddress Target, int Port,string Path )
{
groupEP = new IPEndPoint(Target,Port);
//Fill Fields
_TargetIP=new IPEndPoint(Target,Port);
_UDPPort=Port;
//_soundStream=new NetworkStream(_sock,FileAccess.Write,false);
//get waveinfo
wi=new WaveInfo(Path);
}
public void PlayOnNetwork()
{
try
{
//listener.JoinMulticastGroup(_TargetIP,);
listener.Connect(groupEP);
dataState sd=new dataState(wi.AudioData);
t= new System.Threading.Timer(new TimerCallback(ref sendData),sd,0,200);
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
}
And modifying priority of any app can lead to bad things if you don't
know the exact implications. This is especially true if you move to the
OpenNETCF ThreadEx, as it provides you enough capability to pre-empt even
the kernel. A pre-emption during a GC would be a bad, bad thing.
Thank you all
Giuseppe