<div dir="ltr">Hi Grant,<div><br></div><div>TCP/IP sockets (in general) are designed to be owned by a single thread.<br>It is not permitted to block on the same socket by two different threads.  I don&#39;t know if this is your violation, but it is a problem.</div>
<div>If you want multi-threaded task handlers, the correct socket design is to have one thread handling the socket (8021) reads, and sending the event read to separate task handlers (in parallel).</div><div><br></div><div>
The Receive() method is blocking for an event, and grabbing the first line of the event.</div><div>Does the error occur when Receive() get&#39;s it&#39;s first event, or when the second thread tries to block on a socket that is already owned by thread 1?</div>
<div>If you comment out &quot;Task task2&quot; does the error still occur?<br><br>Ian.</div></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Fri, May 16, 2014 at 7:24 AM, Grant Bagdasarian <span dir="ltr">&lt;<a href="mailto:GB@cm.nl" target="_blank">GB@cm.nl</a>&gt;</span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div lang="EN-US" link="blue" vlink="purple"><div><p class="MsoNormal">Hello,<u></u><u></u></p><p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">I’m trying to run the following code, but after a few seconds I get AccessViolationException:<u></u><u></u></p><p class="MsoNormal"><u></u> <u></u></p><p class="MsoNormal">class Program {<u></u><u></u></p>
<p class="MsoNormal">        static void Main(string[] args) {<u></u><u></u></p><p class="MsoNormal"><u></u> <u></u></p><p class="MsoNormal">            ESLconnection connection = new ESLconnection(&quot;192.168.1.1&quot;, 8021, &quot;ClueCon&quot;);<u></u><u></u></p>
<p class="MsoNormal">            connection.SendRecv(&quot;event plain ALL&quot;);<u></u><u></u></p><p class="MsoNormal">            Task task1 = Task.Run(() =&gt; Receive(1, connection));<u></u><u></u></p><p class="MsoNormal">
            Task task2 = Task.Run(() =&gt; Receive(2, connection));<u></u><u></u></p><p class="MsoNormal">            Console.ReadLine();<u></u><u></u></p><p class="MsoNormal">        }<u></u><u></u></p><p class="MsoNormal">
<u></u> <u></u></p><p class="MsoNormal">        static void Receive(int id, ESLconnection connection) {<u></u><u></u></p><p class="MsoNormal">            while (connection.Connected() == 1) {<u></u><u></u></p><p class="MsoNormal">
                ESLevent eslEvent = connection.RecvEvent();<u></u><u></u></p><p class="MsoNormal">                Console.WriteLine(&quot;{0} - {1}&quot;, id, eslEvent.Serialize(&quot;&quot;).Split(new char[] { &#39;\r&#39;, &#39;\n&#39; })[0]);<u></u><u></u></p>
<p class="MsoNormal">            }<u></u><u></u></p><p class="MsoNormal">        }<u></u><u></u></p><p class="MsoNormal">    }<u></u><u></u></p><p class="MsoNormal"><u></u> <u></u></p><p class="MsoNormal">What the basic idea behind this is, is to have a Task continuously listening for a CHANNEL_HANGUP event, and have the other Task process the call in parallel.<u></u><u></u></p>
<p class="MsoNormal">Does the exception occur because multiple parallel tasks are trying to access the connection object?<u></u><u></u></p><p class="MsoNormal"><u></u> <u></u></p><p class="MsoNormal">Is the only solution to create a second ESLconnection (HANGUP Handler) and register it to receive only calls for a given UUID?<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p><p class="MsoNormal">Regards,<span class="HOEnZb"><font color="#888888"><u></u><u></u></font></span></p><span class="HOEnZb"><font color="#888888"><p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">Grant<u></u><u></u></p></font></span></div></div><br>_________________________________________________________________________<br>
Professional FreeSWITCH Consulting Services:<br>
<a href="mailto:consulting@freeswitch.org">consulting@freeswitch.org</a><br>
<a href="http://www.freeswitchsolutions.com" target="_blank">http://www.freeswitchsolutions.com</a><br>
<br>
FreeSWITCH-powered IP PBX: The CudaTel Communication Server<br>
<a href="http://www.cudatel.com" target="_blank">http://www.cudatel.com</a><br>
<br>
Official FreeSWITCH Sites<br>
<a href="http://www.freeswitch.org" target="_blank">http://www.freeswitch.org</a><br>
<a href="http://wiki.freeswitch.org" target="_blank">http://wiki.freeswitch.org</a><br>
<a href="http://www.cluecon.com" target="_blank">http://www.cluecon.com</a><br>
<br>
FreeSWITCH-users mailing list<br>
<a href="mailto:FreeSWITCH-users@lists.freeswitch.org">FreeSWITCH-users@lists.freeswitch.org</a><br>
<a href="http://lists.freeswitch.org/mailman/listinfo/freeswitch-users" target="_blank">http://lists.freeswitch.org/mailman/listinfo/freeswitch-users</a><br>
UNSUBSCRIBE:<a href="http://lists.freeswitch.org/mailman/options/freeswitch-users" target="_blank">http://lists.freeswitch.org/mailman/options/freeswitch-users</a><br>
<a href="http://www.freeswitch.org" target="_blank">http://www.freeswitch.org</a><br>
<br></blockquote></div><br></div>