开发者

Collection was modified; impossible to perform the operation listings on socket.Receive

When I am doing :

Array.Clear(Core.arrayRead,0 , 1024)
let cBytes = Core.socket.Receive(Core.arrayRead, 1024, SocketFlags.None)

First time (with empty array) it works good, but next time when I am clearing my array to receive new bytes I've got this error : Collection was modified; impossible to perform the operation listings

Is it still clearing ? or what ? How can I fix it ?

more code :

try
    Core.listPos |> Seq.iter /> fun o开发者_如何学Gop ->
        Core.socket.Send(op.Buffer) |> ignore
        Array.Clear(Core.arrayRead,0 , 1024)
        let cBytes = Core.socket.Receive(Core.arrayRead, 1024, SocketFlags.None) // error ->
        if  cBytes > 0 then
            Core.WorkPos cBytes
with
    | :? SocketException as e   -> Core.output.Add ("Ошибка протокола связи : "    + e.Message)
    | _ as e                    -> Core.output.Add ("Ошибка : "                    + e.Message) // My error here 


just wanted to conclude, if someon reads this or got a similiar question in the future:

The problem was the Core.listPos collection and the Seq.iter in

Core.listPos |> Seq.iter /> fun op ->
   ...

not the socket.Receive, because the listPos got changed from another thread. Seq.iter will (just as foreach in C#) throw such exceptions if the collection changes (most often this is due to some .Remove inside the loops-body).

An easy solution would be to write

let positions = Core.listPos |> Seq.toArray
positions |> Seq.iter /> fun op -> ...


You can't modify an enumeration while traversing it (foreach). What is the outer code you are using?

Use a byte[] to receive data, see here for an example.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜