Jump to content
Xtreme .Net Talk

Await event to complete before continuing code


Recommended Posts

Guest WojszaM
Posted

Hi, I have a question. I have a method that sends and receives messengers via serial port as a byte arrays,

 

to SerialPort DataRecived I am adding an event that will read response and assign it to type

 

so basically it should be easy and looks like this:

 

async {

 

let buffer = Array.zeroCreate<byte> port.ReadBufferSize

let response = new Response(buffer)

 

let data_recived =

async{

let! read_buffer = port.AsyncReadLineAsByte()

response.Data <- read_buffer

 

printfn "Response Buffer: %A response type: %A" read_buffer response.Data

}

|> Async.RunSynchronously

 

port.DataReceived.Add(fun _-> data_recived)

 

do! port.AsyncWriteLineAsByte messange

 

//Something to wait for fired event

 

return response.Data

}

 

And type looks like

 

type Response(buffer: byte[]) =

 

let mutable _response = buffer

 

member this.Data

with get() = _response

and set(value) = _response <- value

 

 

Between sending messenge and returning byte array i need something that will wait for event to fire and finish so far i tried:

 

let! args = port.DataReceived |> Async.RunSynchronously

 

But this get stuck, also i tried:

 

while response.Data = buffer

do printfn "Response: %A \r Buffer: %A" response.Data buffer

 

Well this works one time, if I send second message it goes to endless loop like this while is fire before event is fired.

 

Is there any way for this to work?

 

Continue reading...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...