Inhalte aufrufen

Profilbild
- - - - -

Run Async method as ScheduleTask in my plugin

Async ScheduleTask plugin

  • Bitte melden Sie sich an, um eine Antwort zu verfassen.
2 Antworten zu diesem Thema

#1 mohsenns5

mohsenns5

    Newbie

  • Members
  • Punkt
  • 4 Beiträge

Geschrieben: 03 June 2017 - 15:04

Hi guys

 

I want to run async method as a ScheduleTask in my plugin.

 Public class DataPollingTask: ITask
{
        Private static readonly BotClient Bot = new BotClient ();

        Public void Execute (TaskExecutionContext ctx)
        {
            Bot.OnMessage + = BotOnMessageReceived;

            Var me = Bot.GetMeAsync ().

            Bot.StartReceiving ();
        }

        Private static async void BotOnMessageReceived (object sender, MessageEventArgs messageEventArgs)
        {
            // code ...
        }

// other code ...
}

But occurred an error (on this page: Admin / ScheduleTask / List): 

 

"Asynchronous operations asynchronous operations can only be used in an asynchronous handler." If this exception occurs while executing a page, make sure the page is tagged <% @ Page Async = "true"%>. This is an ASP.NET requestprocessing.This is an ASP.NET requestprocessing instead, the asynchronous method should return a task, and the caller should wait for it. "

 

Bot.StartReceiving () is a listener. Bot.StartReceiving () is work in ConsoleProject but I want to use my listener in SmartStore plugin. Plz help and tell me the right way for this purpose.

 
Thanks


#2 Murat Cakir

Murat Cakir

    SmartStore AG

  • Administrators
  • 1118 Beiträge

Geschrieben: 05 June 2017 - 01:14

Bot.GetMeAsync() must be an async method with a return type of Task or Task<something>. Under normal circumstances you would await the mehod call like this:

// Doesn't work, because the Execute() method is not marked as 'async'.
var me = await Bot.GetMeAsync();

// Works!
var me = Task.Run(() => Bot.GetMeAsync() );
me.Wait(); // waits for completion.

You could also wrap your method call like so:

SmartStore.Async.AsyncRunner.RunSync(() => Bot.GetMeAsync());

  • mohsenns5 gefällt das

Murat Cakir
SmartStore AG


#3 mohsenns5

mohsenns5

    Newbie

  • Members
  • Punkt
  • 4 Beiträge

Geschrieben: 05 June 2017 - 16:33

 

Bot.GetMeAsync() must be an async method with a return type of Task or Task<something>. Under normal circumstances you would await the mehod call like this:

// Doesn't work, because the Execute() method is not marked as 'async'.
var me = await Bot.GetMeAsync();

// Works!
var me = Task.Run(() => Bot.GetMeAsync() );
me.Wait(); // waits for completion.

You could also wrap your method call like so:

SmartStore.Async.AsyncRunner.RunSync(() => Bot.GetMeAsync());

Thanks a lot :) . My problem is solved :

Task.Run(() => botItem.StartReceiving());



Auch markiert mit einem oder mehrerer dieser Schlüsselwörter: Async, ScheduleTask, plugin