Start a task in background manually

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
Hace 11 años
I have developed a task, and it's working fine.

I would like to implement also the possibility to run this task once, manually, from the configuration page (for testing purpose) in background (the task take some time to complete).

Does anyone know if it's possible manually execute in background  a nopcommerce task and where  should  I look ?

thanks for any help

Tommaso
Hace 11 años
This is off the top of my head.  I don't have the code base in front of me but I have done this before for testing purposes.

You can call the service method that Execute() uses for your task directly from your controller.

MyService.cs
public void ExecuteMyTask()
{
//do stuff
}


MyController.cs
public MyController(IMyService myService)
{
_myService = myService;
}

public ActionResult ExecuteMyTaskOnce()
{
_myService.ExecuteMyTask();
//other stuff
}


Not sure exactly how you have things setup but this is pretty much all there is too it.  Hope this helps.

t
Hace 11 años
Uhm .... I don't think this is going to run in background / asynchronously ...

this is just to call directly a class that implements ITask interface, and yes, works, but not in background.
Hace 11 años
something like this:


Task.Factory.StartNew( () =>
{
//DO YOUR STUFF                
} );
Hace 11 años
I need the EF of nopcommerce , that is not thread safe... and i have no idea about instantiate a new context....or if is possible...

I'm trying to understand nopcommerce Task logic , but ThreadTask have internal constructors and it's anyway too advanced for my skill...
Hace 11 años
ok.. looks like my dev environment was messed up... I suppose also the other solustion might work


inside the controller you are using place:


public ActionResult StuffToexecuteInBackground(DateTime StartSynchronizeFrom)
{
            var plugin = RetrievePluginInstance();
          

            ThreadPool.QueueUserWorkItem(plugin.Synchronize, objectWithParameters);
          

            return Configure();
}

source: http://csharpfeeds.com/post/5415/Dont_use_the_ThreadPool_in_ASP.NET.aspx
Hace 11 años
strange for some reason


GetCurrentIpAddress() in webhelper class don't like being called in a new thread...
Hace 11 años
You're not in a web context during a task on a separate thread.  There is no HttpRequest to pull that info from.
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.