Search code examples
c#windows-phone-7asmx

Communication between Background(periodic task) and foreground app in windows phone7?


I am new to Windows Phone 7.

I am developing a project in wp7, in that project for every 60 sec I need to send my latitude longitude details to web service(asmx).

I have a background service and am connect to asmx service to it. when I update my details on asmx need to show toast message on phone, but I unable to do it, plz help me

here is my background service code

          protected override void OnInvoke(ScheduledTask task)
               {

        watcher = new GeoCoordinateWatcher(GeoPositionAccuracy.Default);
        watcher.Start();
        string useremail = task.Description.ToString();
        string latitude = watcher.Position.Location.Latitude.ToString();
        string longitude = watcher.Position.Location.Longitude.ToString();

        ServiceReference1.UpdateUserLocationSoapClient obj = new ServiceReference1.UpdateUserLocationSoapClient();
        obj.UpdateUserLocation1Completed += new EventHandler<ServiceReference1.UpdateUserLocation1CompletedEventArgs>(obj_UpdateUserLocation1Completed);
        obj.UpdateUserLocation1Async(useremail, latitude, longitude);   
        // If debugging is enabled, launch the agent again in one minute.
        #if DEBUG_AGENT
        ScheduledActionService.LaunchForTest(task.Name, TimeSpan.FromSeconds(5));
        #endif

        // Call NotifyComplete to let the system know the agent is done working.
        NotifyComplete();
    }

private void obj_UpdateUserLocation1Completed(object sender, ServiceReference1.UpdateUserLocation1CompletedEventArgs e) {

        ShellToast toast = new ShellToast();



        toast.Title = "kk";
        toast.Content = "ss" + e.Result.ToString();
        toast.Show();


    } 

Solution

  • To communicate between your application and your background agent, you should use a Mutex.

    A example : http://www.31a2ba2a-b718-11dc-8314-0800200c9a66.com/2011/11/this-is-continuation-of-this-post.html

    EDIT : Remove NotifyComplete(); from OnInvoke and put it in obj_UpdateUserLocation1Completed