ni.com is currently undergoing scheduled maintenance.

Some services may be unavailable at this time. Please contact us for help or try again later.

Measurement Studio for .NET Languages

cancel
Showing results for 
Search instead for 
Did you mean: 

VisaNS MessageBasedSession BeginWrite is not calling callback async method

Hello team,

 

I was trying the below code in  a C# Winform application , in the writeSession() method once the BeginWrite is done, it needs to call the AsyncCallBack method OnWriteComplete(), which is not happening, I do not see any exception or error. Could you please check if I am doing anything wrong?

 

private MessageBasedSession mbSession;
private IAsyncResult asyncHandle = null;

 

 

private void writeSession(string command)
{
try
{
SetupWaitingControlState(true);
string textToWrite = ReplaceCommonEscapeSequences("?C");
asyncHandle = mbSession.BeginWrite(
textToWrite,
new AsyncCallback(OnWriteComplete),
(object)textToWrite.Length);
OnWriteComplete(asyncHandle);
}
catch (Exception exp)
{
MessageBox.Show(exp.Message);
}
}

 

private string ReplaceCommonEscapeSequences(string s)
{
try
{
return (s != null) ? s.Replace("\\n", "\n").Replace("\\r", "\r") : s;
}
catch (Exception ex)
{
log.Error("Exception in" + MethodBase.GetCurrentMethod(), ex);
return null;
}
}

 

private void OnWriteComplete(IAsyncResult result)
{
try
{
SetupWaitingControlState(false);
mbSession.EndWrite(result);
ioStatusLabel.Text = "I/O Status:" + mbSession.LastStatus.ToString();
writeLabel.Text = "Write: " + ((int)result.AsyncState).ToString();

// Always perform a read after a Write
readSessionsButton_Click(null, null);
}
catch (Exception exp)
{
MessageBox.Show(exp.Message);
}
}

0 Kudos
Message 1 of 3
(2,700 Views)

Hi,

Please ensure you are creating the new AsyncCallback as defined in the documentation for the BeginRead Method

 

Applications Engineering
National Instruments
0 Kudos
Message 2 of 3
(2,682 Views)

Thank you for the reply. If you see my code above, I am using new AsyncCallback as the parameter to BeginWrite().

 

Moreover I have another problem where the below overloaded version of BeginWrite() always returns the AsyncState as null. Below is the code please let me know if anything is wrong. I have the session created as well.

 

string textToWrite = ReplaceCommonEscapeSequences("?C");
byte[] bytes = Encoding.ASCII.GetBytes(textToWrite);
IAsyncResult asyncHandle = mbSession.BeginWrite(bytes,0,bytes.Length);

 

But the below overloaded version of BeginWrite with AsyncCallback code returns a value for AsyncState.

 

asyncHandle = mbSession.BeginWrite(textToWrite, new AsyncCallback(OnWriteComplete),(object)textToWrite.Length);

0 Kudos
Message 3 of 3
(2,621 Views)