Task not running? - .net-4.0

The current problem I'm experiencing, is the when the button is pressed, it seems nothing is happening. I'm unsure of why this happens.
Here is the on click method for the button to be pressed:
private void computeStart_Click(object sender, EventArgs e)
{
_computeTokenSource = new CancellationTokenSource();
GenerateAll(_computeTokenSource.Token);
}
Here is the method called by the on click.
private async void GenerateAll(CancellationToken token)
{
await new Task(() =>
{
var total = (long) Math.Pow(36, 6);
var options = new ParallelOptions {CancellationToken = token};
Parallel.For(0, total, options, a => GenerateCodeAndHash());
}, TaskCreationOptions.LongRunning);
}
Finally this is method called in the Parallel.For
private void GenerateCodeAndHash()
{
var result = new string(
Enumerable.Repeat(Chars, 6)
.Select(s => s[new Random().Next(s.Length)])
.ToArray());
if (_dictionary.ContainsKey(result)) return;
var hash = MD5.Create().ComputeHash(Encoding.UTF8.GetBytes(result));
var sb = new StringBuilder();
for (var j = 0; j < 2; j++)
sb.Append(hash[j].ToString("x2"));
_dictionary.TryAdd(result, sb.ToString());
}

Do not use the Task constructor with async/await.
In this case, you want to use Task.Run:
private async void computeStart_Click(object sender, EventArgs e)
{
_computeTokenSource = new CancellationTokenSource();
await Task.Run(() => GenerateAll(_computeTokenSource.Token));
}
private void GenerateAll(CancellationToken token)
{
var total = (long) Math.Pow(36, 6);
var options = new ParallelOptions {CancellationToken = token};
Parallel.For(0, total, options, a => GenerateCodeAndHash());
}
For more information, see my async/await intro.

The Task constructor creates a Task that is not started. If you want to use it, you need to call Start() afterwards. But most of the time, you want to create and start the Task at the same time, which you can do using Task.Run().
Also, as pointed out by others, you should use async void only in event handlers, nowhere else; the GenerateAll() method should be async Task and you should await it from your event handler, which should be async void.

Related

Subscribe to DisplayAlert in Xamarin.Forms

I'd like to get notified whenever DisplayAlert is called somewhere in my app. The Xamarin.Forms source code suggests to use the MessagingCenter, since it is using it to send a message within DisplayAlert():
MessagingCenter.Send(this, AlertSignalName, args);
But I haven't been able to receive anything, yet. This is one of the lines I tried so far:
MessagingCenter.Subscribe<Page>(this, Page.AlertSignalName, arg => {
Console.WriteLine("Message received: " + arg);
});
Is this the right direction? Or do you have an alternative solution? I'd even consider some hacky reflection-based approach, since I need it for testing purposes only.
This works for me:
MessagingCenter.Subscribe<Page, Xamarin.Forms.Internals.AlertArguments>(this, Page.AlertSignalName, (obj, obj1) => {
int aa = 0;
});
But it is raised when DisplayAlert is displayed. (I think it's correct...)
This is my page:
using System;
using Xamarin.Forms;
namespace Test
{
public class MyPage : ContentPage
{
public MyPage()
{
Button b = new Button {Text = "Press" };
b.Clicked += async (object sender, EventArgs e) => {
await DisplayAlert("Attention", "AAA", "Ok");
MessagingCenter.Send<MyPage>(this, "AAA");
};
Content = new StackLayout
{
Children = {
new Label { Text = "Hello ContentPage" },
b
}
};
MessagingCenter.Subscribe<MyPage>(this, "AAA", (obj) => {
int aa = 0;
});
MessagingCenter.Subscribe<Page, Xamarin.Forms.Internals.AlertArguments>(this, Page.AlertSignalName, (obj, obj1) => {
int aa = 0;
});
}
}
}
In Xamarin source test code (namespace Xamarin.Forms.Core.UnitTests), it's used in a strongly typed way like that:
[Test]
public void DisplayAlert ()
{
var page = new ContentPage ();
AlertArguments args = null;
MessagingCenter.Subscribe (this, Page.AlertSignalName, (Page sender, AlertArguments e) => args = e);
var task = page.DisplayAlert ("Title", "Message", "Accept", "Cancel");
Assert.AreEqual ("Title", args.Title);
Assert.AreEqual ("Message", args.Message);
Assert.AreEqual ("Accept", args.Accept);
Assert.AreEqual ("Cancel", args.Cancel);
bool completed = false;
var continueTask = task.ContinueWith (t => completed = true);
args.SetResult (true);
continueTask.Wait ();
Assert.True (completed);
}
So this should do it:
MessagingCenter.Subscribe(this, Page.AlertSignalName, (Page sender, AlertArguments args) =>
{
Console.WriteLine("Message received: " + args.Message);
});

signalr with sqldepdency firing multiple times for each browser instance

I am having a asp.net mvc app with vs2013 and .net framwork 4.5.1 which should notify users when a certain field gets updated and for a certain recordID.
Everything works fine when I have a single instance of the browser open, but when i open another tab or browser either on the same machine or on the different machine it fires the sqldepdencychange event multiple times.
Below is my hub code
public class MessagesHub : Hub
{
private static string conString = ConfigurationManager.ConnectionStrings["FleetLink_DB"].ToString();
private static string hostName = "";
public static void SendMessages(string hName)
{
IHubContext context = GlobalHost.ConnectionManager.GetHubContext<MessagesHub>();
hostName = hName;
context.Clients.Group(hostName).updateMessages(hName);
}
public Task leaveGroup(string hName)
{
return Groups.Remove(Context.ConnectionId, hName);
}
public Task joinGroup(string hName)
{
return Groups.Add(Context.ConnectionId, hName);
}
}
Below is my signalr script file
$(function () {
var dialog, form
// Declare a proxy to reference the hub.
var notifications = $.connection.messagesHub;
//debugger;
//Create a function that the hub can call to broadcast messages.
notifications.client.updateMessages = function (hName) {
alert("testing");
getoneMessages(hName)
};
$.connection.hub.logging = true;
$.connection.hub.start().done(function () {
var hostName = getUrlVars()["System_Name"];
notifications.server.joinGroup(hostName);
}).fail(function (e) {
alert(e);
});
});
function getUrlVars() {
var vars = [], hash;
var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
for (var i = 0; i < hashes.length; i++) {
hash = hashes[i].split('=');
vars.push(hash[0]);
vars[hash[0]] = hash[1];
}
return vars;
}
function getoneMessages(hName) {
var tbl = $('#selectable');
//alert('mesgID=' + mesgID)
//var tbl = $('#selectable');
$.ajax({
url: '/controller/view',
cache: false,
contentType: 'application/html ; charset:utf-8',
type: 'GET',
dataType: 'html'
}).success(function (result) {
//alert(result);
tbl.empty().append(result);
}).error(function (exception) {
//alert('failed= ' + exception);
});
}
window.onbeforeunload = function (e) {
var hostName = getUrlVars()["System_Name"];
notifications.server.joinGroup(hostName);
$.connection.hub.stop();
};
Below is my partialview code along with the definition for RegisterForNotification and depdendency_onchange event
public PartialViewResult SignalRTesterPartialView()
{
/...COde not included for brevity..../
RegisterForNotifications(ID);
}
public void RegisterForNotifications(int mID)
{
var efConnectionString = ConfigurationManager.ConnectionStrings["DB"].ConnectionString;
var builder = new EntityConnectionStringBuilder(efConnectionString);
var regularConnectionString = builder.ProviderConnectionString;
string commandText = null;
commandText = "select ID,Status,Name from tblABC where ID=" + strID;
using (SqlConnection connection = new SqlConnection(regularConnectionString))
{
using (SqlCommand command = new SqlCommand(commandText, connection))
{
connection.Open();
var dependency = new SqlDependency(command);
dependency.OnChange += new OnChangeEventHandler(dependency_OnChange);
// NOTE: You have to execute the command, or the notification will never fire.
var reader = command.ExecuteReader();
}
}
}
private void dependency_OnChange(object sender, SqlNotificationEventArgs e)
{
if (e.Type == SqlNotificationType.Change && e.Info== SqlNotificationInfo.Update)
{
MessagesHub.SendMessages(hName);
}
RegisterForNotifications(1012);
}
Not sure why it is firing the sendmessages multiple times with each additional browser instance that I open. Any pointers would be helpful!
remove EventHandler when you done with it
private void dependency_OnChange(object sender, SqlNotificationEventArgs e)
{
if (e.Type == SqlNotificationType.Change && e.Info== SqlNotificationInfo.Update)
{
MessagesHub.SendMessages(hName);
}
//remove event handler
SqlDependency dependency = sender as SqlDependency;
dependency.OnChange -= new OnChangeEventHandler(dependency_OnChange);
RegisterForNotifications(1012);
}

How to dynamically bind event to command in WinRT without Reactive framework?

I'm implementing modified version of Behavior for my Windows 8 app according to this guide. It works except one place where the Reactive framework is required:
protected override void OnAttached()
{
var evt = AssociatedObject.GetType().GetRuntimeEvent(Event);
if (evt != null)
{
Observable.FromEventPattern<RoutedEventArgs>(AssociatedObject, Event)
.Subscribe(se => FireCommand());
}
base.OnAttached();
}
The question is simple, how to achieve similar funcitonality without the Reactive frmaework? I've browsed the source of of Rx that can be obtained here, but I'ts just too complicated to me.
I've also succeeded porting to code with the only problem that it work only for fixed type of EventHandler:
protected override void OnAttached()
{
EventInfo evt = AssociatedObject.GetType().GetRuntimeEvent(Event);
if (evt != null)
{
AssignEvent<ItemClickEventHandler>(AssociatedObject, Event, FireCommand);
}
base.OnAttached();
}
protected void AssignEvent<T1>(object instance, string eventName, T1 handler)
{
EventInfo runtimeEvent = instance.GetType().GetRuntimeEvent(eventName);
Func<T1, EventRegistrationToken> add = a => (EventRegistrationToken)runtimeEvent.AddMethod.Invoke(instance, new object[] { a });
Action<EventRegistrationToken> remove = a => runtimeEvent.RemoveMethod.Invoke(runtimeEvent, new object[] { a });
WindowsRuntimeMarshal.AddEventHandler(add, remove, handler);
}
Any idea, how to make it dynamic so I don't have to use specific event handler "ItemClickEventHandler"? Note in classic .NET it' quite simple, but in WinRT I cannot use Delegate.CreateDelegate(...)
Update:
Thanks to Brandon I was able to finish the method, it now looks like this:
protected override void OnAttached()
{
EventInfo evt = AssociatedObject.GetType().GetRuntimeEvent(Event);
if (evt != null)
{
MethodInfo addMethod = evt.AddMethod;
MethodInfo removeMethod = evt.RemoveMethod;
ParameterInfo[] addParameters = addMethod.GetParameters();
Type delegateType = addParameters[0].ParameterType;
Action<object, object> handler = (s, e) => FireCommand(e as RoutedEventArgs);
MethodInfo handlerInvoke = typeof(Action<object, object>).GetRuntimeMethod("Invoke", new[] { typeof(object), typeof(object) });
Delegate #delegate = handlerInvoke.CreateDelegate(delegateType, handler);
Func<object, EventRegistrationToken> add = a => (EventRegistrationToken)addMethod.Invoke(AssociatedObject, new object[] { #delegate });
Action<EventRegistrationToken> remove = t => removeMethod.Invoke(AssociatedObject, new object[] { t });
WindowsRuntimeMarshal.AddEventHandler(add, remove, handler);
}
base.OnAttached();
}
Now I can remove 800kB of Rx dlls, thanks again!
I trolled through the Rx source, and here is the important bit of functionality:
MethodInfo addMethod = eventInfo.GetAddMethod();
MethodInfo removeMethod = eventInfo.GetRemoveMethod();
var addParameters = addMethod.GetParameters();
var delegateType = addParameters[0].ParameterType;
Action<object, object> handler = (object sender, object eventArgs) => FireCommand();
MethodInfo handlerInvoke = typeof(Action<object, object>).GetMethod("Invoke");
Delegate delegate = handlerInvoke.CreateDelegate(delegateType, handler);
Func<EventRegistrationToken> add = a => (EventRegistrationToken)addMethod.Invoke(instance, new object[] { delegate });
Action<EventRegistrationToken> remove = t => removeMethod.Invoke(instance, new object[] { t });
It looks like the important info is they are using MethodInfo.CreateDelegate.

Call MessageDialog from Property Changed handler Store App

I'm trying to call a MessageDialog out of a PropertyChanged Handler. The first call is always successful, but when the Dialog gets called a second time, I get an UnauthorizedAccessException.
I've tried to wrap the call in a Dispatcher, but I got the same behavior.
Here's the code (snippet of MainPage.xaml.cs):
void PropertyChanged(object sender, PropertyChangedEventArgs e)
{
await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
{
showMessage("Message", "Title");
});
}
async void showMessage(String message, String title)
{
MessageDialog dialog = new MessageDialog(message, title);
await dialog.ShowAsync();
}
Could anybody please help me with this issue?
I think your problem is that multiple property changes will cause multiple calls to display the dialog. You should only ever display one dialog at a time:
bool _isShown = false;
async void showMessage(String message, String title)
{
if (_isShown == false)
{
_isShown = true;
MessageDialog dialog = new MessageDialog(message, title);
await dialog.ShowAsync();
_isShown = false;
}
}

Windows Store App UI - not responsive

I'm trying to get my head around designing a UI that remains responsive while a long running task is being executed.
To that end, I created a simple app in VS2012 and added the following class to it:
using System.Threading.Tasks;
namespace TaskTest
{
class Class1
{
public async Task<int> Async()
{
//simulate a long running process
for (long x = 0; x < long.MaxValue; x++) { }
return 1;
}
}
}
I then modified the main page's LoadState() method thusly:
protected override async void LoadState(Object navigationParameter, Dictionary<String, Object> pageState)
{
await DoLongRunningProcess();
}
private async Task DoLongRunningProcess()
{
var id = 0;
id = await new Class1().Async();
await new MessageDialog(id + "").ShowAsync();
}
I want the page to remain responsive while that process executes. However, when I run this code, the page takes a long time to load. What am I doing wrong?
TIA
async isn't magic; it just gives you the capability to write asynchronous code. In particular, async does not execute code on a background thread. You can use Task.Run to do this.
You may find my async/await intro or the MSDN documentation helpful.
That was helpful. I made the following changes and I got the result I was looking for:
class Class1
{
public int Launch()
{
//throw new Exception("Class1 exception");
for (var i = 0; i < int.MaxValue / 2; i++) ;
return 1;
}
}
...
protected async override void LoadState(Object navigationParameter, Dictionary<String, Object> pageState)
{
var task = DoLongRunningProcess();
await task;
await new MessageDialog(task.Result + "").ShowAsync();
}
private Task<int> DoLongRunningProcess()
{
return Task.Run<int>(() => new Class1().Launch());
}
The page continues to load and after a short pause the message dialog is displayed. Now however, I need to know how to catch exceptions. If I uncomment the //throw new Exception ... line in method Launch(), it is reported as an unhandled exception. I want to catch this exception in the main UI thread (i.e., in the body of method LoadState) but I can't seem to manage it.