C# Making Async Calls using the iControlAssembly
Problem this snippet solves:
This C# example application shows three different ways to make iControl calls Asynchronously using the An iControlAssembly library. Each way serves a slightly different purpose depending on the needs of the application.
Code :
using System;
using System.Collections.Generic;
using System.Text;
using iControl;
using System.Diagnostics;
using System.Threading;
namespace iControlAssemblyAsyncExample
{
class AsyncExample
{
private static Thread mainThread;
static void Main(string[] args)
{
Stopwatch sw = null;
Interfaces m_Interfaces = null;
IAsyncResult ar = null;
string[] poolList = null;
////////////////////////////////////////////////////////////////////////////////
// Synchronously connect to the BigIP Appliance
Console.WriteLine("Establishing connection to Appliance.");
sw = Stopwatch.StartNew();
m_Interfaces = new Interfaces("hostname", "username", "password");
Console.WriteLine("Connection completed in {0} ms\n", sw.ElapsedMilliseconds);
////////////////////////////////////////////////////////////////////////////////
// Synchronously Retrieve the Pool List
Console.WriteLine("Beginning syncronous Pool list retrieval.");
sw = Stopwatch.StartNew();
poolList = m_Interfaces.LocalLBPool.get_list();
Console.WriteLine("Syncronous retrieval completed in {0} ms\n", sw.ElapsedMilliseconds);
////////////////////////////////////////////////////////////////////////////////
// Asynchronously retrieve the Pool List, do something and then wait
// for the Async call to complete.
Console.WriteLine("Beginning async retrieval with wait.");
sw = Stopwatch.StartNew();
ar = m_Interfaces.LocalLBPool.Beginget_list(null, null);
// Do some work, which may or may not take longer than
// the Async call to execute.
Console.WriteLine("Done some work after {0} ms. Now waiting.",sw.ElapsedMilliseconds);
ar.AsyncWaitHandle.WaitOne(); // Wait indefinitely for the Async Call to complete
ar.AsyncWaitHandle.Close();
poolList = m_Interfaces.LocalLBPool.Endget_list(ar);
Console.WriteLine("Asyncronous retrieval completed in {0} ms\n", sw.ElapsedMilliseconds);
////////////////////////////////////////////////////////////////////////////////
// Asynchronously Retrieve the Pool List while doing something indefinitely.
Console.Write("Beginning inline async retrieval");
sw = Stopwatch.StartNew();
ar = m_Interfaces.LocalLBPool.Beginget_list(null, null);
while (ar.IsCompleted == false)
{
Console.Write("."); // Continually to something until the Async
Thread.Sleep(15); // call has completed.
}
poolList = m_Interfaces.LocalLBPool.Endget_list(ar);
Console.WriteLine("\nAsyncronous retrieval completed in {0} ms\n", sw.ElapsedMilliseconds);
////////////////////////////////////////////////////////////////////////////////
// Asynchronously Retrieve the Pool List with a Callback
AsyncCallback myCallBackRoutine = new AsyncCallback(MyAsyncCallback);
mainThread = Thread.CurrentThread;
Console.WriteLine("Beginning Callback Async retrieval");
sw = Stopwatch.StartNew();
ar = m_Interfaces.LocalLBPool.Beginget_list(myCallBackRoutine, m_Interfaces);
try
{
while (true)
{
Console.Write("."); // Do something useful while we
Thread.Sleep(15); // wait for the Callback to complete.
}
}
catch (ThreadInterruptedException)
{
Console.WriteLine("\nAsyncronous Callback retrieval completed in {0} ms\n", sw.ElapsedMilliseconds);
}
Console.WriteLine("Press ANY key to exit.");
Console.ReadKey();
}
private static void MyAsyncCallback(IAsyncResult ar)
{
Interfaces m_Interfaces = (Interfaces)ar.AsyncState;
string[] poolList = m_Interfaces.LocalLBPool.Endget_list(ar);
int c = 0;
while (c < 7)
{
Console.Write("X"); // Do something useful while the main
Thread.Sleep(15); // thread is doing what it needs to
c++;
}
mainThread.Interrupt();
}
}
}Published Mar 07, 2015
Version 1.0Rowan_110725
Nimbostratus
Joined January 17, 2011
Rowan_110725
Nimbostratus
Joined January 17, 2011
1 Comment
- Jason_Jones
Nimbostratus
Do this examples work with version 11.5?