Thursday, November 5, 2009

The calling thread cannot access the object because different thread owns it

Hello Friends as i have mentioned the error in the Title has been solved.I m posting it on the net so that you can get help from it.Please also see the links given below.

///
/// This method is getting voice input from microphone
/// Hardeep Bhullar
/// 1/11/2009
///
///
/// Description Of Change
///

private void Voice_In()
{
try
{
bool firstTime = true;
_sendingPortText = (string)this.Dispatcher.Invoke(new Func(() => this.TextBoxSendingPort.Text));
byte[] br;
//this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, (Action)(() =>
//{
_socket.Bind(new IPEndPoint(IPAddress.Any, int.Parse(_sendingPortText)));
//}));
while (true)
{
br = new byte[16384];
if (firstTime)
{
firstTime = false;
//Thread.Sleep(250);
}

_socket.Receive(br);
m_Fifo.Write(br, 0, br.Length);
}
}
catch (Exception ex) { }
}

///
/// This method is passing out the recorded voice to the speakers
/// Hardeep Bhullar
/// 1/11/2009
///
///
/// Description Of Change
///

private void Voice_Out(IntPtr data, int size)
{
try
{
string strOtherMachineIP = (string)this.Dispatcher.Invoke(new Func(() => this.TextBox1.Text));
//for Recorder
if (m_RecBuffer == null || m_RecBuffer.Length < size)
m_RecBuffer = new byte[size];
System.Runtime.InteropServices.Marshal.Copy(data, m_RecBuffer, 0, size);
IPEndPoint m_endPoint;
//this.Dispatcher.Invoke(DispatcherPriority.Normal, (Action)(() =>
//{
//Microphone ==> data ==> m_RecBuffer ==> m_Fifo
//get the end point.
m_endPoint = new IPEndPoint(IPAddress.Parse(strOtherMachineIP), int.Parse(_sendingPortText));
// _socket.SendTo(m_RecBuffer, new IPEndPoint(IPAddress.Parse(strOtherMachineIP), int.Parse(sendingPortText)));
_socket.SendTo(m_RecBuffer, m_endPoint);
//}
//));
}
catch (Exception Ex) { }
}

http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/885d6b03-0699-472b-867b-e526c93d8fb8/