Proper exception handling on Task.Wait() where initial task must complete first - exception

I'm not quite understanding the exception handling in await-able tasks. I have a primary task that needs to complete and yield its result to dependent tasks. So, the primary task must complete first, then, the subsequent tasks can run asynchronously. If the initial/primary task fails and throws an exception, I need to handle the exception properly and exit the program. If the 2 dependent tasks hit any exceptions, I just need to report them properly. Two things are happening in the code below... 1) Task 2 and 3 are running synchronously (I want them to run asynchronously)... AND 2) if I uncomment the exception throw inside DoStringStuff, I will get the "Exception User-Unhandled" message and the program stops (and I want this exception to bubble up to the top like normal and simply be printed to the console). I'm missing some important semantics here. Any help in the right direction is much appreciated!
UPDATE:
I found the issue, if this will help anyone... I needed to declare all my tasks at the outset of the function (not dynamically). Below is the working code. The Exceptions (if any) will propagate up as expected.
await parent();
Console.ReadKey();
static async Task parent()
{
Console.WriteLine("start of parent");
string sResult;
try
{
sResult = MyTask1("1111");
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
return;
}
try
{
string result = await DoRemainingTasks(sResult);
Console.WriteLine();
Console.WriteLine(result);
}
catch(Exception ex)
{
Console.Write(ex.ToString());
}
Console.WriteLine("end of parent");
}
static async Task<string> DoRemainingTasks(string sResult)
{
Task<string> task2 = MyTask2(sResult);
Task<string> task3 = MyTask3("3333");
string t2Result = string.Empty;
string t3Result = string.Empty;
try
{
t2Result = await task2;
}
catch(Exception ex)
{
Console.WriteLine("Task 2 issue. Error = " + ex.Message);
}
try
{
t3Result = await task3;
}
catch (Exception ex)
{
Console.WriteLine("Task 3 issue. Error = " + ex.Message);
}
return "Remaining tasks done: " + t2Result + "... " + t3Result;
}
static Task<string> DoStringStuff(string s)
{
foreach (char c in s)
{
Console.Write(c);
Thread.Sleep(500);
}
Console.WriteLine();
return Task.FromResult(s);
}
static async Task<string> DoStringStuffAsync(string s)
{
return await Task.Run(() =>
{
foreach (char c in s)
{
Console.Write(c);
Thread.Sleep(500);
}
Console.WriteLine();
return s;
});
//throw new Exception($"task {s} failure");
}
static string MyTask1(string svar)
{
try
{
DoStringStuff(svar);
Console.WriteLine();
Console.WriteLine("task 1 done");
}
catch
{
throw;
}
return "2222";
}
static async Task<string> MyTask2(string svar)
{
return await Task.Run(async () =>
{
string s = await DoStringStuffAsync(svar);
return "task done for " + s;
});
}
static async Task<string> MyTask3(string svar)
{
return await Task.Run(async () =>
{
string s = await DoStringStuffAsync(svar);
return "task done for " + s;
});
}

Related

DNN API Controller - A task was canceled Log

We have an JQuery Ajax call that will execute when a user is about to leave a page on a DNN module.
This error is being logged the whole time in the DNN logs.
How can I improve the error handling so that it doesn't log the whole time?
Here is the DNN log:
Here is the Front End Code on the Module :
$(window).on("beforeunload", function () {
ClearTempUserSessionWhenLeavePage();
});
function ClearTempUserSessionWhenLeavePage() {
if ($fromButtonEvent == false) {
var Url = $.fn.GetBaseURL() + 'DesktopModules/DNNCommon/API/Store/ClearTempUserSessionWhenLeavePage';
$.ajax({
url: Url,
type: 'GET',
async: true,
dataType: 'json',
success: function () {
},
error: function (x, y, z) {
}
}).promise().done(function () {
});
}
$fromButtonEvent = false;
}
We are inheriting the DNNApiController class on our DNNCommon class.
This is the C# method being called:
[AllowAnonymous]
[HttpGet]
public void ClearTempUserSessionWhenLeavePage()
{
if (SessionManager.GetSessionObject("NewCreatedWebUser") != null)
{
System.Web.HttpContext.Current.Session["DoNotRemoveSessionIfNotAuthenticated"] = false;
SessionManager.SetSessionObject("NewCreatedWebUser", null);
SessionManager.SetSessionObject("UserInfo", null);
SessionManager.SetSessionObject("NewCustomerCode", null);
}
}
I have attempted to add two different types of Try Catch clauses, but when I debug the code it won't hit the breakpoints and somehow it still logs the error in the DNN Admin logs. Is there perhaps a Try Catch in the DNNController class that is writing this error?
First attempt with Try Catch Clause with TaskCanceledException and TimeoutException:
[AllowAnonymous]
[HttpGet]
public void ClearTempUserSessionWhenLeavePage()
{
try
{
if (SessionManager.GetSessionObject("NewCreatedWebUser") != null)
{
System.Web.HttpContext.Current.Session["DoNotRemoveSessionIfNotAuthenticated"] = false;
SessionManager.SetSessionObject("NewCreatedWebUser", null);
SessionManager.SetSessionObject("UserInfo", null);
SessionManager.SetSessionObject("NewCustomerCode", null);
}
}
catch (Exception ex)
{
EventLogController logController = new EventLogController();
if (ex.InnerException is TimeoutException)
{
ex = ex.InnerException;
}
else if (ex is TaskCanceledException)
{
if ((ex as TaskCanceledException).CancellationToken == null || (ex as TaskCanceledException).CancellationToken.IsCancellationRequested == false)
{
ex = new TimeoutException("Timeout occurred");
logController.AddLog("Timout Occured - Clearing Temp User Session When Leave Page.", ex.ToString(), EventLogController.EventLogType.ADMIN_ALERT);
}
}
logController.AddLog("Problem Clearing Temp User Session When Leave Page.", ex.ToString(), EventLogController.EventLogType.ADMIN_ALERT);
}
}
Second attempt with a TaskCanceledException:
[AllowAnonymous]
[HttpGet]
public void ClearTempUserSessionWhenLeavePage()
{
try
{
if (SessionManager.GetSessionObject("NewCreatedWebUser") != null)
{
System.Web.HttpContext.Current.Session["DoNotRemoveSessionIfNotAuthenticated"] = false;
SessionManager.SetSessionObject("NewCreatedWebUser", null);
SessionManager.SetSessionObject("UserInfo", null);
SessionManager.SetSessionObject("NewCustomerCode", null);
}
}
catch (TaskCanceledException ex)
{
EventLogController logController = new EventLogController();
logController.AddLog("Task Cancelled Exception - Clearing Temp User Session When Leave Page.", ex.ToString(), EventLogController.EventLogType.ADMIN_ALERT);
}
}

How to return an Error from a generic method

The title doesn't describe the issue very well so let me explain my problem.
I have a generic function to consume API's via HTTP GET calls. Which looks like this:
public async static Task<T> GetAsync<T>(string Base_Url,string relative_URL, Utility.UriExtensions.NameValueCollection Params, Utility.UriExtensions.NameValueCollection headers = null)
{
Uri CompleteURL = new Uri(Base_Url + relative_URL, UriKind.Absolute);
if (Params != null)
CompleteURL = Utility.UriExtensions.CreateUriWithQuery(CompleteURL, Params);
if(headers!=null)
{
foreach(KeyValuePair<string,string> k in headers)
{
if (ApiHttpClient.DefaultRequestHeaders.ContainsKey(k.Key))
ApiHttpClient.DefaultRequestHeaders[k.Key] = k.Value;
else
ApiHttpClient.DefaultRequestHeaders.Add(k.Key, k.Value);
}
}
Debug.WriteLine("GET : " + CompleteURL);
using (var response = await ApiHttpClient.GetAsync(CompleteURL).AsTask(cancellationToken.Token).ConfigureAwait(false))
{
string responseData="";
if (response.IsSuccessStatusCode)
{
responseData = await response.Content.ReadAsStringAsync();
Debug.WriteLine(responseData);
JsonSerializerSettings settings = new JsonSerializerSettings();
settings.DateParseHandling = DateParseHandling.DateTime;
settings.DefaultValueHandling = DefaultValueHandling.Populate;
settings.NullValueHandling = NullValueHandling.Include;
settings.TypeNameHandling = TypeNameHandling.None;
}
try
{
response.EnsureSuccessStatusCode();
return JsonConvert.DeserializeObject<T>(responseData);
}
catch
{
// Error
Debug.WriteLine(
"Error occurred, the status code is: {0} and Content : {1}",
response.StatusCode, response.Content);
}
return default(T);
}
}
In case of error, the error is handled inside catch body. I want to return these Error informations like StatusCode and Content of response when this happens. But I am unable to make any changes to this generic function. How should handle this.
For this purpose you can use out parameter, which you will pass in your method:
public async static Task<T> GetAsync<T>(string Base_Url,string relative_URL, Utility.UriExtensions.NameValueCollection Params, Utility.UriExtensions.NameValueCollection headers = null, out StatusCode code)
StatusCode statusCode;
GetAsync<Foo>(..., out statusCode);

Object Synchronization method was called from unsynchronized block while using Mutex

I am Trying to create a Windows Universal App with a Background Task.
I am trying to Write a Background task that Triggers Upon an Incoming bluetooth connection.
To prevent both the foreground and background from creating a connection. I am trying to implement the Same Mutex in Both the Foreground and Background.
When I read the Data from the Bluetooth Device I am getting the Following Error.
Object Synchronization method was called from unsynchronized block
To my Surprise this Error comes Occasionally . Am I missing Something ?
Here's My Code :
public sealed class RFBackgroundTask : IBackgroundTask
{
.... Variable declarations
public async void Run(IBackgroundTaskInstance taskInstance)
{
BackgroundTaskDeferral deferral = taskInstance.GetDeferral();
try
{
taskInstance.Canceled += new BackgroundTaskCanceledEventHandler(OnCanceled);
Debug.WriteLine("RFComm Task Running");
hotwatchConnection = taskInstance.TriggerDetails as RfcommConnectionTriggerDetails;
socket = hotwatchConnection.Socket;
reader = new DataReader(socket.InputStream);
// n = new Notification(hotwatchConnection.RemoteDevice.HostName);
await Read();
}
catch (System.Exception e)
{
Debug.WriteLine("RFComm Task Error: {0}", e.Message);
if (ismutexReleased == false)
{
Debug.WriteLine("Releaseing mutex because of error {0}:", e.Message);
connectionMutex.ReleaseMutex();
ismutexReleased = true;
}
}
finally
{
if (ismutexReleased == false)
{
Debug.WriteLine("Releasing Mutex 2");
connectionMutex.ReleaseMutex();
}
ismutexReleased = true;
}
deferral.Complete();
}
public IAsyncAction Read()
{
return Task.Run(async () =>
{
try
{
connectionMutex = new Mutex(false, CONNECTION_MUTEX_NAME);
// Attempt to wait for the mutex
var waitResult = connectionMutex.WaitOne();
if (waitResult)
{
Debug.WriteLine("Aquired Mutex Successfully");
}
// If the wait was not successful, fail the connect operation
if (!waitResult) { throw new TimeoutException(); }
if (reader != null)
{
uint length = 500;
reader.InputStreamOptions = InputStreamOptions.Partial;
uint len = await reader.LoadAsync(length);
String message = reader.ReadString(len);
Debug.WriteLine("Read " + message + " In the First Attemnpt");
var roamingSettings = Windows.Storage.ApplicationData.Current.RoamingSettings;
roamingSettings.Values["COMMAND"] = message;
//if(!message.StartsWith("01"))
//{
// await ProcessCommand(message);
//}
}
reader.Dispose();
socket.Dispose();
socket = null;
if (waitResult == true)
connectionMutex.ReleaseMutex();
ismutexReleased = true;
Debug.WriteLine("Released Mutex successfully after reading data");
}
catch (Exception e)
{
Debug.WriteLine(e.Message);
if (ismutexReleased == false)
{
Debug.WriteLine("Releaseing mutex because of error {0}:", e.Message);
connectionMutex.ReleaseMutex();
ismutexReleased = true;
}
throw;
}
finally
{
if (ismutexReleased == false)
{
connectionMutex.ReleaseMutex();
Debug.WriteLine("Releasing Mutex");
}
ismutexReleased = true;
}
}).AsAsyncAction();
}
}
The Mutex must be released on the same thread that it was acquired.
When you await an async method you are returned to the same context, you are not guaranteed to be returned to the same thread.
Use a Semaphore instead.

Receive data from arduino through bluetooth in windows phone 8

i found this example(https://developer.nokia.com/Community/Wiki/Windows_Phone_8_communicating_with_Arduino_using_Bluetooth) in my research to develop a bluetooth console to windows phone 8. This example work very well, except for the TERMINATE function. When i call TERMINATE function, the ReceiveMessages function still trying receive data, but there is no more socket available and it generate a system.exception. I tried a lot of workaround, but i dont have enough experience with C#, this is my first APP. Anyone know how can i workaround this situation or have a better example?
i did only 1 modificiation:
private async void AppToDevice()
{
if (!connected)
{
ConnectAppToDeviceButton.Content = "Connecting...";
PeerFinder.AlternateIdentities["Bluetooth:Paired"] = "";
var pairedDevices = await PeerFinder.FindAllPeersAsync();
if (pairedDevices.Count == 0)
{
Debug.WriteLine("No paired devices were found.");
}
else
{
foreach (var pairedDevice in pairedDevices)
{
if (pairedDevice.DisplayName == DeviceName.Text)
{
connectionManager.Connect(pairedDevice.HostName);
ConnectAppToDeviceButton.Content = "Disconnect";
DeviceName.IsReadOnly = true;
//ConnectAppToDeviceButton.IsEnabled = false;
continue;
}
}
}
}
else
{
connectionManager.Terminate();
ConnectAppToDeviceButton.Content = "Connect";
}
}
I found a solution here:
WinRT: DataReader.LoadAsync Exception with StreamSocket TCP
I did only a few modifications:
public void Terminate()
{
try
{
if (socket != null)
{
taskLoadLength.Cancel();
taskLoadLength.Close();
taskLoadMessage.Cancel();
taskLoadMessage.Close();
socket.Dispose();
dataReadWorker.CancelAsync();
dataReader.Dispose();
dataWriter.Dispose();
isInicialized = false;
}
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
}
private void ReceiveMessages(object sender, DoWorkEventArgs ev)
{
while (true)
{
try
{
// Read first byte (length of the subsequent message, 255 or less).
//uint sizeFieldCount = await dataReader.LoadAsync(1);
taskLoadLength = dataReader.LoadAsync(1);
taskLoadLength.AsTask().Wait();
uint sizeFieldCount = taskLoadLength.GetResults();
if (sizeFieldCount != 1)
{
// The underlying socket was closed before we were able to read the whole data.
return;
}
// Read the message.
uint messageLength = dataReader.ReadByte();
taskLoadMessage = dataReader.LoadAsync(messageLength);
taskLoadMessage.AsTask().Wait();
uint actualMessageLength = taskLoadMessage.GetResults();
//uint actualMessageLength = await dataReader.LoadAsync(messageLength);
if (messageLength != actualMessageLength)
{
// The underlying socket was closed before we were able to read the whole data.
return;
}
// Read the message and process it.
string message = dataReader.ReadString(actualMessageLength);
MessageReceived(message);
}
catch (AggregateException ae)
{
MessageBox.Show(ae.Message);
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
}
}

StackOverflow exception

I am using DDE client to attach and listen stock market prices. That client has a callback method I implemented what to do when it receives price changes. The problem is that I get StackOverflowException (periodically and not at the same time interval). I found something about Thread.BeginCriticalRegion(), but I'm not sure if it would help. I have a few more hours until market opening when I can test it.
I would be more than greatful if someone could give me an idea how to override this exception.
Thanks in advance,
Aleksandar
IList<SymbolObject> _symbols; //initialized when the app runs for the first time
void _ddeClient_Advise(object sender, DdeAdviseEventArgs args)
{
if (!IsReady)
return;
if (string.IsNullOrEmpty(args.Text))
{
_logMessages.LogMessagesAdd("advise dde symbol", string.Format("args.Text is empty or NULL for {0}", args.Item), true);
return;
}
try
{
string[] argsArray = args.Text.Replace("\0", "").Replace('\0'.ToString(), "").Split(' '); // sometimes happens here
var list = _symbols.Where(s => s.DDESymbol == args.Item).ToList();
if (list.Count == 0)
return;
decimal? val = null;
try
{
var stringParts = StringUtils.CleanProphitXUrl(argsArray[0]).Split('.');
argsArray = null;
if (stringParts.Length >= 2)
val = decimal.Parse(stringParts[0] + "." + (stringParts[1].Length > 2 ? stringParts[1].Substring(0, 2) : stringParts[1]));
else
val = decimal.Parse(stringParts[0]);
stringParts = null;
}
catch (Exception ex)
{
_logMessages.LogMessagesAdd("call Price Alerts application service", ex.Message, true);
return;
}
foreach (var l in list)
{
if (_lastPrices[l.DDESymbol] == null)
continue;
if (_lastPrices[l.DDESymbol].ToString() != val.ToString())
{
try
{
_quotePublishingService.PublishQuote(l.DDESymbolId, l.Symbol, args.Item, val, WebSyncPublisherUrl,
PublishingChannel); // a call to wcf service
}
catch (Exception ex)
{
_logMessages.LogMessagesAdd("call the service", ex.Message, true); // save to sql db
return;
}
_lastPrices[l.DDESymbol] = val.ToString();
}
}
list = null;
val = null;
}
catch
{
}
}
public static string CleanProphitXUrl(string value) // StringUtils.CleanProphitXUrl snippet
{
StringBuilder sb = new StringBuilder();
sb.Append(value.Substring(0, value.LastIndexOf(".") + 1));
try
{
value = value.Replace('\r'.ToString(), "").Replace('\t'.ToString(), "").Replace('\n'.ToString(), "");
for (int i = sb.Length; i < value.Length; i++)
{
if (char.IsNumber(value[i]))
sb.Append(value[i]);
}
}
catch
{
}
return sb.ToString();
}
A StackOverflowException is caused by making to many method calls usually resulting from unintended recursion. Based on a cursory check of the code you posted I do not believe it is the culprit. The problem likely lies somewhere else.