Getting a stream asynchronously (C#)
Posted: Tue Jun 13, 2006 2:00 pm
Until BlueTools natively allows for an asynchronous call to RemoteService.Stream this class below can be used as a work-around.
It used like this:
The OnStreamReceived method will then be called when the RemoteService.Stream returns a Stream.
The method should look like this:
Below is the class (just copy and paste into a file):
Regards,
Jonas
Franson Support
It used like this:
Code: Select all
// Where you usually try to get the Stream synchronously. StreamAsync async = new StreamAsync(m_serviceCurrent); async.OnStreamReceived += new ObjectPushSample.StreamAsync.StreamAsyncEventHandler(async_OnStreamReceived); async.Parent = this; // if you want the event to be called in the same thread async.GetStreamAsync();
The method should look like this:
Code: Select all
private void async_OnStreamReceived(object sender, ServiceStream stream) { // do what you want with the Stream here }
Code: Select all
// // (c) 2006 Franson Technology AB, Sweden, All rights reserved // // franson.com // // Written by Jonas Sandman // // This code is provided merely as an example. // No guarantees are made for its functionality or stability. // using System; namespace StreamAsync { /// <summary> /// This class let you get a BlueTools Stream asynchronously. /// </summary> public class StreamAsync { public delegate void StreamAsyncEventHandler(object sender, Franson.BlueTools.ServiceStream stream); // Public event, which is called when the stream is received public event StreamAsyncEventHandler OnStreamReceived; // Private variables // // remote service that we want a stream from private Franson.BlueTools.RemoteService m_service = null; // parent for invoke private System.Windows.Forms.Control m_control = null; /// <summary> /// Constructor for StreamAsync class /// </summary> /// <param name="service">A reference to the RemoteService that you wish to get a Stream to.</param> public StreamAsync(Franson.BlueTools.RemoteService service) { m_service = service; } // Call this method to attempt to receive a Stream public void GetStreamAsync() { System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(Run)); } public System.Windows.Forms.Control Parent { get { return m_control; } set { m_control = value; } } // Threaded method private void Run(object obj) { Franson.BlueTools.ServiceStream stream = m_service.Stream; // if event handler is set we call it // the event handler MUST be set, otherwise we won't receive the Stream! if (OnStreamReceived != null) { // if parent is set - we invoke the call for thread safety if (Parent != null) { Parent.Invoke(OnStreamReceived, new object[] { this, stream }); } else { // if the parent is not set - we just call it OnStreamReceived(this, stream); } } } } }
Jonas
Franson Support