rexmont
Posts: 11
Joined: Mon Jan 12, 2009 5:14 pm

Calling currentService.Stream repeatedly

Mon Jan 26, 2009 2:09 pm

Hello,

In my app, I have to call that line repeatedly:

currentStream = currentService.Stream;

It takes almost 800 msec to evaluate that line and I'm calling the "writeCallBack" many times, so my app's speed drops down.

How can I speed up the process? I have tried to define a global stream variable and assign it to "currentStream" when I get currentService.Stream for the first time.

Thanks in advance,

Uygar.

Jonas
Posts: 2319
Joined: Mon Jan 09, 2006 2:20 pm
Location: Sweden

RE: Calling currentService.Stream repeatedly

Mon Jan 26, 2009 2:41 pm

The first time you call it, you will connect to the remote device. That probably takes about 800 ms, but then you should just use the returned stream. Are you doing something in your code which invalidates currentStream?

Regards,
Jonas

rexmont
Posts: 11
Joined: Mon Jan 12, 2009 5:14 pm

RE: Calling currentService.Stream repeatedly

Mon Jan 26, 2009 6:09 pm

Jonas thanks, let me explain,

I initially call "WCB2Global" function. Then it calls "writecallback2" and I read the response from "readcallback2" function. I look into data (exactly some bytes of data) and if, for example, it is 0x61 I do something, then after doing something I call "WCB2Global" one more time, then "writecallback2" and then "readcallback2", look into data... etc. and so on. I paid attention to ;

if (currentService != null)
{
currentStream.Close();
currentService = null;
currentStream = null;
}

This is closing the stream, I think.

You can see the generic write function, "WCB2Global", derived from your findandconnect sample.

private void WCB2Global(char[] envelope)
{
if (currentService != null)
{
currentStream.Close();
currentService = null;
currentStream = null;
}
currentService = (RemoteService)serviceList.SelectedItem;
char[] gonder = envelope;
byte[] gonderArr = new byte[gonder.Length];
for (int j = 0; j < gonder.Length; j++)
gonderArr[j] = (byte)gonder[j];

try
{
currentStream = currentService.Stream;
currentStream.BeginWrite(gonderArr, 0, gonderArr.Length, new AsyncCallback(writeCallback2), currentStream);
}
catch (Exception exc)
{
//MessageBox.Show(exc.Message);
showErrorPanel(exc.Message + "\n" + exc.TargetSite + "\n" + exc.Data);
currentService = null;
}
}

You can see the "writecallback2" function below;

private void writeCallback2(IAsyncResult result)
{

BlueToolsAsyncResult blueAsyncResult = (BlueToolsAsyncResult)result;

ServiceStream stream = (ServiceStream)blueAsyncResult.AsyncState;

try
{
stream.EndWrite(result);
System.Threading.Thread.Sleep(300);
stream.BeginRead(m_buffer, 0, m_buffer.Length, new AsyncCallback(readCallback2), stream);

}
catch (ObjectDisposedException ex)
{
closeConnection.Enabled = false;
serviceList.Enabled = true;
}
}

and the "readcallback2" function;

private void readCallback2(IAsyncResult result)
{
byte[] buffer = null;
BlueToolsAsyncResult blueAsyncResult = (BlueToolsAsyncResult) result;
ServiceStream stream = (ServiceStream) blueAsyncResult.AsyncState;
buffer = blueAsyncResult.Buffer;

try
{
int len = stream.EndRead(result);

if (Convert.ToInt32(buffer[1]) == 103)
{

/*
DoSomething();
WCB2Global(...); //Calls again.
*/

}
else if (Convert.ToInt32(buffer[1]) == 117)
{

/*
DoSomethingAgain();
WCB2Global(...); //Calls again_2.
*/
}

else if (Convert.ToInt32(buffer[1]) == 98)
{
/*
DoSomethingOneMoreTime();
WCB2Global(...); //Calls again_3.
*/

}
stream.BeginRead(m_buffer, 0, m_buffer.Length, new AsyncCallback(readCallback2), stream);
}
catch(ObjectDisposedException ex)
{
closeConnection.Enabled = false;
serviceList.Enabled = true;
}
}
Thank you,

Uygar.

Jonas
Posts: 2319
Joined: Mon Jan 09, 2006 2:20 pm
Location: Sweden

RE: Calling currentService.Stream repeatedly

Tue Jan 27, 2009 9:13 am

Well, since you call WCB2Global constantly which is closing the Stream it's little surprise you are experiencing slowdowns. You are connecting, reading a few bytes, disconnecting and then connecting again, starting it all over.

Regards,
Jonas

Return to “BlueTools”