1

I have bulk of records to send push notifications by mobile device to other mobile devices.

For that I am using for loop inside task.Start(). I wonder whether task.Start() running in background or not? So that while sending remote notifications to mobile device at the same time I could do some other stuff too & it wont block mobile UI.

The code below I am using

var pushTask = new Task(() =>
{
    if (myPushDataFilterd.Any())
    {
        var title = txtHomeworkTitle.Value.Trim();
        for (int index = 0; index < myPushDataFilterd.Count; index++)
        {
            var row = myPushDataFilterd[index];

            jData.Add("moduleName", "Homework");
            jData.Add("organizationId", ddlOrganization.SelectedValue);
            jData.Add("studentId", studentId);
            jGcmData.Add("to", to);
            jGcmData.Add("data", jData);
            api = row.ServerKeyPush;                                       

            var url = new Uri("https://fcm.googleapis.com/fcm/send");

            using (var client = new HttpClient())
            {
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                client.DefaultRequestHeaders.TryAddWithoutValidation("Authorization", "key=" + api);
                var r = client.PostAsync(url, new StringContent(jGcmData.ToString(), Encoding.Default, "application/json")).Result;
            }
        }
    }
});
pushTask.Start();

Actually this the web application part I am now using in mobile application. In mobile application do I have other better options too, to send notification?

AustinWBryan
  • 3,249
  • 3
  • 24
  • 42
R15
  • 13,982
  • 14
  • 97
  • 173
  • 1
    You should use `Task.Run` instead of `new Task()`. And it will run on a different thread from a ThreadPool. – FCin Jul 30 '18 at 10:27
  • oh so isn't it good approach? – R15 Jul 30 '18 at 10:29
  • See https://stackoverflow.com/questions/29693362/regarding-usage-of-task-start-task-run-and-task-factory-startnew – Rui Jarimba Jul 30 '18 at 10:31
  • You create task, but run `client.PostAsync` without awaiting it(`.Result` is synchronous). All of this could could be written without creating task at all. You should mark your method `async Task` and await `client.PostAsync`. – FCin Jul 30 '18 at 10:33

2 Answers2

1

Task in general represents an asynchronous operation, so code inside a task block won't block unless you wanted to read its value by explicitly waiting, check the following for example:

  Task myTask = new Task( () => Console.WriteLine("It is me myTask ^_^ "));
  myTask.Start();
  Console.WriteLine("Currently not waiting the output from myTask");
  myTask.Wait();//Now I am waiting
  //Output:
  //Currently not waiting the output from myTask
  //It is me myTask ^_^ 

Also you can create and start a task in one statement using Task.Run & TaskFactory.StartNew. For more information about the differences in usage between them check the link.

Ali Ezzat Odeh
  • 2,093
  • 1
  • 17
  • 17
1

There is no point in creating task at all. You are doing IO operations, so you can make use of already provided async api.

private async Task PostData()
{
    if (myPushDataFilterd.Any())
    {
        var title = txtHomeworkTitle.Value.Trim();
        using (var client = new HttpClient())
        {
            for (int index = 0; index < myPushDataFilterd.Count; index++)
            {
                var row = myPushDataFilterd[index];

                jData.Add("moduleName", "Homework");
                jData.Add("organizationId", ddlOrganization.SelectedValue);
                jData.Add("studentId", studentId);
                jGcmData.Add("to", to);
                jGcmData.Add("data", jData);
                api = row.ServerKeyPush;                                       

                var url = new Uri("https://fcm.googleapis.com/fcm/send");
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                client.DefaultRequestHeaders.TryAddWithoutValidation("Authorization", "key=" + api);
                var r = await client.PostAsync(url, new StringContent(jGcmData.ToString(), Encoding.Default, "application/json"));
            }
        }
    }
}
FCin
  • 3,804
  • 4
  • 20
  • 49