C# Changes: New applications to VBVoice

The simple, object-oriented programming language of C# is VBVoice™’s main development language. What do recent Microsoft® changes to C# mean to VBVoice developers? In this blog post we’re highlighting two fairly new functionalities – switching using the type pattern and asynchronous programming with async and await keywords – and explaining how they can be applied to VBVoice.

Switching using the type pattern that was introduced in C# 7.0 allows VBVoice developers to write generic code to serve their interactive voice response (IVR) applications. Consider the function to initialize all the VBVoice controls in your application that would do the following:

  • Remove every entry greeting from all PlayGreeting controls
  • Set all GetDigits controls to retry four times
  • Change every Delay control to play mymusic.wav file

As there are many ways to initialize those controls, the most concise way using a small number of lines would be using the newly extended switch statement of C# 7.0 as follows:

private void init(object sender, EventArgs e)
{
foreach (var ctrl in vbvFrame1.Controls)
{
switch (ctrl)
{
case PlayGreeting pg:
{
var greet = (Greeting)pg.EntryGreeting;
greet.RemoveAll();
}
break;
case GetDigits gd:
gd.MaxRetries = 4;
break;
case Delay dy:
{
var greet = (Greeting)dy.MusicGreeting
greet.RemoveAll();
greet.InsertFile(0, “mymusic.wav”);
dy.IDelayTime = 15;
}
break;
}
}
}

The switch statement not only provides code branching depending on the type control, but also properly casts it to the correct type to facilitate accessing its members later.

The second feature we would like to highlight is the asynchronous programming with async and await keywords that were added in C# 5.0, which simplifies the way to run asynchronous tasks along the IVR call flow and provide a quick way of multitasking that is clean and straightforward. Consider your need to fetch the homepage of www.pronexus.com and look for the latest version available for VBVoice, the code to retrieve and parse the webpage is:

var client = new HttpClient();
var page = client.GetStringAsync(“http://www.pronexus.com”).Result;
var m = Regex.Match(str, @”VBVoice [0-9\.]+”, RegexOptions.IgnoreCase);
if (m.Success)
;// m.Value

This code is blocking the calling thread while retrieving the result property, which makes it not suitable to be called from the main thread of a VBVoice application and we need to use an asynchronous task to execute it. Let’s use a delay control named delay1 and put this code in its EnterEvent:

private async void delay1_EnterEvent(object sender, AxVBVoiceLib._DDelayEvents_EnterEvent e)
{
// this code executes first
vbvFrame1.TransferValue[“WebResponse”, e.channel] = “”;
var page = await Task.Run(() =>
{
// this code executes on another thread while delay1_EnterEvent event exits and the delay music started playing
var client = new HttpClient();
return client.GetStringAsync(“http://www.pronexus.com”);
});
// this code executes after all the code in the other thread is done
var m = Regex.Match(page, @”VBVoice [0-9\.]+”, RegexOptions.IgnoreCase);
if (!m.Success)
vbvFrame1.TransferValue[“WebResponse”, e.channel] = “n/A”;
else
vbvFrame1.TransferValue[“WebResponse”, e.channel] = m.Value;
if (lineGroup1.IsCurrentControl(e.channel, delay1))
{
((Pronexus.VBVoice.Delay)sender).StopDelay(e.channel);
}
}

Note that we marked this event with the keyword async, which tells the compiler to return this thread and proceed with other events as soon as there is a call to an awaitable task. That task we created is using an anonymous function that returns all the text in the page to the page variable. The code then proceeds on the main thread to quickly parse the text looking for the word “VBVoice” followed by a number that looks like a version next to it. We used a WebResponse transfer property to save the results to be used somewhere in the call flow.

Because the second half of the event handler executes later, we check to see whether the delay was done (the IsCurrentControl is a custom extension method that returns true if the call flow is still inside the delay1 control. Read about the use of extension methods with VBVoice here), and then finally it stops the delay for the call flow to proceed.

How are you applying C# changes to VBVoice? We’d like to hear your thoughts.

from blog

Related News

Make the Most of C# Programming Features: Build On to VBVoice with Extension Methods

For developers creating IVR applications with VBVoice™, there are many approaches to expanding object functionality. A feature of the C# programming language, Extension Methods, allows you to expand an object’s Learn More

Experiencing Call Collisions? We Have a Solution!

No matter the care with which complex or extensive telecommunications systems are developed, inherent obstacles to providing flawless inbound and outbound interactions are introduced through SIP stack delays, slow PBXs, Learn More

Don’t Wait to Benefit from Pronexus Customer Support

Pronexus’ support team is always here to help – and sometimes, this means being proactive. That is why our excellent support staff keep a record of commonly asked questions or Learn More