Esempio n. 1
0
        /// <summary>
        /// This is a wrapper for the asynchronous operation of querying bidi.
        /// </summary>
        /// <returns>IAsyncOperation so that this method is:
        ///     1) awaitable in C#.
        ///     2) consumed as a WinJS.promise on which we can then use .then() or .done().</returns>
        public IAsyncOperation <string> GetInkLevelAsync()
        {
            return(AsyncInfo.Run(async(o) =>
            {
                // This allows the app to wait until a certain event is fired before continuing on.
                // To prevent the waiting from locking up the entire computer, this is used in an async task.
                AutoResetEvent waitHandler = new AutoResetEvent(false);

                string queryString = "\\Printer.Consumables";

                // Define the delegate for when OnBidiResponseReceived is invoked.
                printerQueue.OnBidiResponseReceived += ((sender, responseArguments) =>
                {
                    bidiResponse = ParseResponse(responseArguments);

                    // Set the waitHandler to signal that a response has been received.
                    waitHandler.Set();
                });

                // Send the query.
                printerQueue.SendBidiQuery(queryString);

                // Asynchronously wait for the the waitHandler to be set.
                await Task.Run(() =>
                {
                    waitHandler.WaitOne();
                });

                return bidiResponse;
            }));
        }
        /// <summary>
        /// Retrieve the ink level, and raise the onInkLevelReceived event when data is available.
        /// </summary>
        public void SendInkLevelQuery()
        {
            printerQueue.OnBidiResponseReceived += OnBidiResponseReceived;

            // Send the query.
            string queryString = "\\Printer.Consumables";
            printerQueue.SendBidiQuery(queryString);
        }