SMS background task get COM ERROR E_CHANGED_STATE 0x8000000C RRS feed

  • Question

  • Hi

    Basically i'm using msft's sample code SMS background task sample and add the Save function(every time sms received, save it to local file)  in the run method in SampleSmsBackgroundTask.cs

    It works fine, but every 1 out of 5, i got this  exception:A concurrent or interleaved operation changed the state of the object, invalidating this operation. (Exception from HRESULT: 0x8000000C). In what circumstances will this E_CHANGED_STATE exception happened?

    I caught this exception in my Save function 

    Here is my code snippet

    public async void Save(IBackgroundTaskInstance taskInstance)
                //get the storage for your app
                StorageFolder localFolder = ApplicationData.Current.LocalFolder;
                StorageFile timeOrderFile = await localFolder.CreateFileAsync("output.txt", CreationCollisionOption.OpenIfExists);
                string output = await FileIO.ReadTextAsync(timeOrderFile);
                output+="bachground is on.\n";
                SmsReceivedEventDetails smsDetails = (SmsReceivedEventDetails)taskInstance.TriggerDetails;


                    SmsDevice smsDevice = (SmsDevice)await SmsDevice.FromIdAsync(smsDetails.DeviceId);
                    SmsBinaryMessage smsEncodedmsg = (SmsBinaryMessage)await smsDevice.MessageStore.GetMessageAsync(smsDetails.MessageIndex);
                    SmsTextMessage smsTextMessage = Windows.Devices.Sms.SmsTextMessage.FromBinaryMessage(smsEncodedmsg);
                    output+=smsTextMessage.Body+" "+" "+smsTextMessage.From+" "+smsTextMessage.Timestamp+"\r\n";
                catch (Exception e)
                    output +="error="+e.Message+"\r\n";
                await FileIO.WriteTextAsync(timeOrderFile, output);

            public async void Run(IBackgroundTaskInstance taskInstance)
                // Associate a cancellation handler with the background task.

                taskInstance.Canceled += new BackgroundTaskCanceledEventHandler(OnCanceled);
                // Do the background task activity.
                BackgroundTaskDeferral deferral = taskInstance.GetDeferral();
                await DisplayToastAsync(taskInstance);
                    // Provide status to application via local settings storage
                    var settings = Windows.Storage.ApplicationData.Current.LocalSettings;
                    settings.Values[taskInstance.Task.TaskId.ToString()] = "Completed";

                Debug.WriteLine("Background " + taskInstance.Task.Name + ("process ran"));

    Saturday, October 13, 2012 10:43 AM

All replies

  • Hi,

    Does anyone have an answer to this question? I'm having the same problem (only 5 out of 5 times), after receiving a sms message, i'm trying to get the SmsTextMessage, but it failes on 

    SmsDevice smsDevice = (SmsDevice)await SmsDevice.FromIdAsync(smsDetails.DeviceId);

    I'm having my background task set in my Package.appxmanifest

    <Extension Category="windows.backgroundTasks" EntryPoint="XXX.Tasks.SmsNotificationTask">
            <Task Type="systemEvent" />

    Should I set something in my service metadata?

    I hope someone can help!

    Thanks in advance

    • Edited by Tyraiden Monday, March 4, 2013 3:18 PM
    Monday, March 4, 2013 2:48 PM