How to execute multiple DB calls asynchronously using Simple.Data - asp.net

I am using Simple.Data with SQL Server and I have multiple methods that are independent of each other. On a form I have multiple drop down lists that need to be populated and I think these can be populated asynchronously. I have a repository that returns List of entities.
From my Asp.Net website I call the methods on the repository one by one and bind them to the drop down lists here is a sample code
private void Initialize()
{
LoadTechnologies();
LoadInstallationTypes();
LoadProvinces();
LoadYears();
}
private void LoadTechnologies()
{
ddlTechnologies.DataSource = _GizRepository.GetTechnologies();
ddlTechnologies.DataValueField = "Name";
ddlTechnologies.DataTextField = "Name";
ddlTechnologies.Items.Insert(0, new ListItem("All", "-1"));
ddlTechnologies.DataBind();
}
private void LoadInstallationTypes()
{
ddlInstallationType.DataSource = _GizRepository.GetInstallationTypes();
ddlInstallationType.DataValueField = "Type";
ddlInstallationType.DataTextField = "Type";
ddlInstallationType.Items.Insert(0, new ListItem("Any", "-1"));
ddlInstallationType.DataBind();
}
private void LoadProvinces()
{
ddlProvinces.DataSource = _GizRepository.GetProvinces();
ddlProvinces.DataValueField = "Name";
ddlProvinces.DataTextField = "Name";
ddlProvinces.Items.Insert(0, new ListItem("All", "-1"));
ddlProvinces.DataBind();
}
private void LoadYears()
{
ddlYearFrom.DataSource = _GizRepository.GetYears();
ddlYearFrom.DataValueField = "Year";
ddlYearFrom.DataTextField = "Year";
ddlYearFrom.DataBind();
ddlYearTo.DataSource = _GizRepository.GetYears();
ddlYearTo.DataValueField = "Year";
ddlYearTo.DataTextField = "Year";
ddlYearTo.DataBind();
}
You can see from the code above that all I am doing is fetching some lists from the repository and bind them to the drop downs. I want to execute these methods asynchronously instead of synchronously, Kindly guide how it can be done?

Use async/await but don't return a Task from each routine. The effect is that they run concurrently all awaiting on their own io and then complete on the UI thread.
private void Initialize()
{
LoadTechnologies();
LoadInstallationTypes();
LoadProvinces();
LoadYears();
}
// Note that LoadTechnologies will return as soon as it is
// called. The part after the await will be scheduled on
// the UI thread after the task completes with the data
private async Task LoadTechnologies()
{
ddlTechnologies.Datasource =
await Task.Run(()=>GizRepository.GetTechnologies());
ddlTechnologies.DataValueField = "Name";
ddlTechnologies.DataTextField = "Name";
ddlTechnologies.Items.Insert(0, new ListItem("All", "-1"));
ddlTechnologies.DataBind();
}
private async Task LoadInstallationTypes()
{
...
}
...
}

At the moment there's no way to make Simple.Data do proper asynchronous database calls. Work is starting on version 2 next week, and the first new feature to be added is proper asynchronous operations for those back ends - such as SQL Server - where the ADO provider has asynchronous methods.

Related

Method checks for lock, runs after lock released

Im facing a potential chase error. Im using javafx TableView to display my data, and I'm periodically receiving an update request externally which calls my update function. I also have some listeners which does stuff such as handle mousedrag events etc. What i want to do is to do something like this:
private void handleEvent(){
TableView.setRowFactory(new Callback<TableView<MyModel>, TableRow<MyModel>>(){
public TableRow<MyModel> call(TableView<MyModel> p) {
final TableRow row = new TableRow();
row.setOnDragDetected(new EventHandler<MouseEvent>(){
public void handle(){
//implement some kind of lock to prevent receiving data update
}
}
row.setOnMouseDragExited(new EventHandler<MouseDragEvent>(){
//release lock to accept update
}
}
}
//this method is being called externally periodically
public void updateModel(MyModel model){
//this won't work because it will skip entirely if it's locked,
//I want it to instead run later when lock is released
if (!locked){
this.model = model;
}
}
I did a quick workaround by using a Boolean to lock and unlock as shown in updateModel Method, problem with that is it will lose some updated data because it's skipped entirely.. instead, I want it to run later when lock is released.. how can I implement this kind of lock mechanism and run later feature?
Edit: why I suspect this is because my listeners are manipulating and getting table data.. while the data is constantly updated, I'm not sure if this is causing my table to break.
Just write some logic that collects everything you tried to do in a locked state and executes it on unlocking.
The following code assumes you're using Platform.runLater or similar code that makes the update run on the application thread.
public class UpdateSynchronizer {
private final List<Runnable> pendingUpdates = new ArrayList<>();
private boolean locked = false;
public void lock() {
if (locked) {
throw new IllegalStateException("double lock");
} else {
locked = true;
}
}
public void runUpdate(Runnable updater) {
if (updater == null) {
throw new IllegalArgumentException();
}
if (locked) {
pendingUpdates.add(updater);
} else {
updater.run();
}
}
public void unlock() {
for (Runnable r : pendingUpdates) {
try {
r.run();
} catch(Exception ex) {
ex.printStackTrace(); // print but ignore
}
}
pendingUpdates.clear();
locked = false;
}
}
If the last update always overwrites all the data from previous updates, simply keeping a single Runnable instead of a list of them would be more performant.
private final UpdateSynchronizer synchronizer = new UpdateSynchronizer();
// why did all the keywords start with uppercase letters (compile time error)
private void handleEvent(){
TableView.setRowFactory(new Callback<TableView<myModel>, TableRow<myModel>>(){
public TableRow<myModel> call(TableView<myModel> p) {
final TableRow row = new TableRow();
row.setOnDragDetected(new EventHandler<MouseEvent>(){
public void handle(){
synchronizer.lock();
//implement some kind of lock to prevent receiving data update
}
}
row.setOnMouseDragExited(new EventHandler<MouseDragEvent>(){
//release lock to accept update
synchronizer.unlock();
}
}
}
//this method is being called externally periodically
public void updateModel(myModel model){
synchronizer.runUpdate(() -> {
// this is just an assignment and won't have any side effects
// updates to the scene may only happen, if the model is accessed in some event handler or animation
this.model = model;
});
}

Set a job to failed using Hangfire with ASP.NET?

I have an ASP.NET app which sends emails whenever the user signs up in the web site. I'm using hangfire in order to manage the jobs and postal in order to send emails.
It all works great, but here's the thing:
I want the superuser to change how many times the APP can send the email before deleting the job.
Here's my code
public static void WelcomeUser(DBContexts.Notifications not)
{
try{
var viewsPath = Path.GetFullPath(HostingEnvironment.MapPath(#"~/Views/Emails"));
var engines = new ViewEngineCollection();
engines.Add(new FileSystemRazorViewEngine(viewsPath));
Postal.EmailService service = new Postal.EmailService(engines);
WelcomeUserMail welcomeUserMail = new WelcomeUserMail();
welcomeUserMail.To = not.ReceiverEmail;
welcomeUserMail.UserEmail = not.ReceiverEmail;
welcomeUserMail.From = BaseNotification.GetEmailFrom();
service.Send(welcomeUserMail);
}
catch(Exception e)
{
DBContexts.DBModel dbModel = new DBModel();
DBContexts.Notifications notificacionBD = dbModel.Notifications.Find(not.NotificationID);
notificacionBD.Status = false;
notificacionBD.Timestamp = DateTime.Now;
notificacionBD.Error = e.Message;
int numberOfRetriesAllowed = ParameterHelper.getNumberOfRetriesAllowed();
if (notificacionBD.Retries > numberOfRetriesAllowed)
{
//In this case Hangfire won't put this job in the failed section but rather in the processed section.
dbModel.SaveChanges();
}
else
{
notificacionBD.Retries++;
dbModel.SaveChanges();
throw new Exception(e.Message);
}
}
}
Why not just add attributes to handle it automatically?
[AutomaticRetry(Attempts = 10, LogEvents = true, OnAttemptsExceeded = AttemptsExceededAction.Delete)]
public void MyTask(){
//doing stuff
}
Or you could just make your own attribute that mimics the AutommaticRetryAttribute class but you handle it how you want?
https://github.com/HangfireIO/Hangfire/blob/a5761072f18ff4caa80910cda4652970cf52e693/src/Hangfire.Core/AutomaticRetryAttribute.cs

Write to entity framework database within Task.Factory.StartNew

How can I save something to my database in the entity framework within a separate thread?
What I want to do could look like that, but obviously doesn't work like that:
public class ThingsHandler : IHttpHandler
{
private DatabaseContext db = new DatabaseContext();
private void ProcessThing()
{
System.Threading.Tasks.Task.Factory.StartNew(() =>
{
Thing dbThing = db.Things.Where(...).AsEnumerable().LastOrDefault();
dbThing.some_property = true;
db.SaveChanges();
}
}
I hope, I was able to make clear, what I would like to achieve. But what do I need to do?
If you want to stay with task, you should place context initialization inside task:
System.Threading.Tasks.Task.Factory.StartNew(() =>
{
var db = new DatabaseContext();
Thing dbThing = db.Things.Where(...).AsEnumerable().LastOrDefault();
dbThing.some_property = true;
db.SaveChanges();
}
But if you use EF6, you might want to use Async methods:
private async void ProcessThing()
{
Thing dbThing = await db.Things.Where(...).AsEnumerable().LastOrDefaultAsync();
dbThing.some_property = true;
await db.SaveChangesAsync();
}

Workflow application.PersistableIdle event not firing

Hi I am new to Windows Workflow. This may be very easy, but I am stuck on this from long.
I have a state machine workflow, in which i have a workflow host class.
Persistence is not working in this code. While debugging pointer never goes to application.persistableIdle event.
I use custom input argument, for which I have set as Serializable.
below is my code of the host class:
static InstanceStore instanceStore;
static AutoResetEvent instanceUnloaded = new AutoResetEvent(false);
static Activity activity = new Activity1();
static Guid id = new Guid();
static int intContractHeaderKey;
static Contract contract = new Contract();
public ContractActivityHost(Guid wfid, Int32 contractHeaderID)
{
SetupInstanceStore();
StartAndUnloadInstance(contractHeaderID);
if (intContractHeaderKey > 0)
{
LoadAndCompleteInstance(id, intContractHeaderKey);
}
}
static void StartAndUnloadInstance(Int32 contractHeaderID)
{
contract = new Contract();
//var objContract = new object();
var input = new Dictionary<string, object>
{
{"TheContract", contract}
};
input.Add("ContractHeaderKey", contractHeaderID);
WorkflowApplication application = new WorkflowApplication(activity, input);
application.InstanceStore = instanceStore;
//returning IdleAction.Unload instructs the WorkflowApplication to persists application state and remove it from memory
application.PersistableIdle = (e) =>
{
return PersistableIdleAction.Unload;
};
application.Unloaded = (e) =>
{
instanceUnloaded.Set();
};
//application.Idle = (e) =>
// {
// //application.Unload();
// instanceUnloaded.Set();
// };
//This call is not required
//Calling persist here captures the application durably before it has been started
application.Persist();
id = application.Id;
application.Run();
instanceUnloaded.WaitOne();
//application.Unload();
//contract = (Contract)objContract;
intContractHeaderKey = contract.ContractID;
}
static void LoadAndCompleteInstance(Guid wfid, Int32 contractHeaderID)
{
//string input = Console.ReadLine();
while (!contract.ContractWFPause)
{
contract.FireContract(contract.ContractID);
WorkflowApplication application = new WorkflowApplication(activity);
application.InstanceStore = instanceStore;
application.Completed = (workflowApplicationCompletedEventArgs) =>
{
//Console.WriteLine("\nWorkflowApplication has Completed in the {0} state.", workflowApplicationCompletedEventArgs.CompletionState);
strWFStatus = "Completed";
};
application.Unloaded = (workflowApplicationEventArgs) =>
{
//Console.WriteLine("WorkflowApplication has Unloaded\n");
strWFStatus = "Unloaded";
instanceUnloaded.Set();
};
application.Load(wfid);
instanceUnloaded.WaitOne();
}
}
private static void SetupInstanceStore()
{
instanceStore =
new SqlWorkflowInstanceStore(#"Data Source=.;Initial Catalog=WorkflowInstanceStore;Integrated Security=True;");
InstanceHandle handle = instanceStore.CreateInstanceHandle();
InstanceView view = instanceStore.Execute(handle, new CreateWorkflowOwnerCommand(), TimeSpan.FromSeconds(10));
handle.Free();
instanceStore.DefaultInstanceOwner = view.InstanceOwner;
}
I have been trying to resolve this from long time, but not sure where I am missing anything. I have gone through couple of sample applications and changed my code to match the flow and logic, but still it does not work.
After application.persist, record is inserted in [System.Activities.DurableInstancing].[InstancesTable] view.
But debug pointer does not move beyond instanceUnloaded.WaitOne();
it actually goes to idle state. if I uncomment application.idle event, it goes in that event code.
Any help to resolve this would be great.
Thanks.
Please check If you have added the below details
instanceStore = new SqlWorkflowInstanceStore(ConfigurationManager.ConnectionStrings["WFPersistenceDb"].ConnectionString);
StateMachineStateTracker.Promote(this.instanceStore);

Threading for methods on button click

I am calling few methods on a button click.
functionA()
functionB()
functionC()
All three functions are independent from each other and they take long time to execute. I checked and found that by threading I can run all three together which will save the execution time.
As I am new to threading concept, could anyone please guide me the simplest way I can do threading in scenario or other way which will be useful in this scenario.
EDIT
One more problem in the same function:
I am binding 5 gridviews after the three functions execution. Like this
gv1.DataSource = GetData("Mill");
gv1.DataBind();
gv2.DataSource = GetData("Factory");
gv2.DataBind();
gv3.DataSource = GetData("Garage");
gv3.DataBind();
gv4.DataSource = GetData("Master");
gv4.DataBind();
They all are using the same method for getting the result and they are also taking time to load. Is there any way I can run them parallel too? I afraid, because they are using same method to get the data. Is it possible to do threading for them. How ?
I am not sure how Parallel.Invoke() decides what to execute in parallel, but if you want an assurance that they will execute in parallel, use threads:
var t1 = new Thread(MySlowFunction);
t1.IsBackground = true;
t1.Start();
var t2 = new Thread(MySlowFunction);
t2.IsBackground = true;
t2.Start();
# To resync after completion:
t1.Join();
t2.Join();
Or even better, use the ThreadPool:
ThreadPool.QueueUserWorkItem(MyWork);
Remember to handle your thread exceptions.
The simplest answer is to use MSDN: Parallel.Invoke().
You should also consider: Asynchronous Pages.
Try using System.Threading.Tasks namespace
Something like
var task1 = Task.Factory.StartNew(() => DoA());
var task2 = Task.Factory.StartNew(() => DoB());
var task3 = Task.Factory.StartNew(() => DoC());
Task.WaitAll(task1, task2, task3);
http://www.codethinked.com/net-40-and-systemthreadingtasks
Here's an example which will execute the 4 tasks in parallel:
public partial class _Default : System.Web.UI.Page
{
public class MyViewModel
{
public int Id { get; set; }
public string Name { get; set; }
}
protected void Page_Load(object sender, EventArgs e)
{
}
protected void BtnBindClick(object sender, EventArgs e)
{
// we define the input for the tasks: each element consists
// of the grid we are willing to bind the results at the end and
// some optional parameter we want to pass to the GetData function
var inputs = new[]
{
new { Grid = gv1, Input = "Mill" },
new { Grid = gv2, Input = "Factory" },
new { Grid = gv3, Input = "Garage" },
new { Grid = gv4, Input = "Master" },
};
// define the tasks we want to execute in parallel
var tasks = inputs
.Select(x => Task.Factory.StartNew(
() => new { Grid = x.Grid, Output = GetData(x.Input) })
)
.ToArray();
// define a task which will be executed once all tasks have finished
var finalTask = Task.Factory.ContinueWhenAll(tasks, x => x);
// wait for the final task
finalTask.Wait();
// consume the results
foreach (var item in finalTask.Result)
{
if (item.Exception == null)
{
// if no exception was thrown for this task we could bind the results
item.Result.Grid.DataSource = item.Result.Output;
item.Result.Grid.DataBind();
}
}
}
private MyViewModel[] GetData(string input)
{
// Simulate slowness
Thread.Sleep(1000);
return Enumerable.Range(1, 5).Select(x => new MyViewModel
{
Id = x,
Name = input
}).ToArray();
}
}

Resources