1

I would like to ask for some suggestions on how I can make the following below possible:

Functionality wanted: I want to give users the option to enable and disable the startup setting for a desktop bridge application I am building while the application is running. Ideally, a small Form window will pop up when a button is pressed via a contextmenu giving two options to the user. 'yes' or 'no' buttons being the two options for whether the app should startup automatically at boot time for the next restart of the system.

Technologies being used: I built the app as a winform application and made it into a uwp app via the desktop bridge functionality. I did this because it will eventually be on the windows app store.

References: there is an API that I am referencing which is called StartupTask that I found at this site: https://learn.microsoft.com/en-us/uwp/api/windows.applicationmodel.startuptask#properties

I already have this in my Package.appxmanifest file:

<Extensions>
<desktop:Extension
Category="windows.startupTask"
Executable="uniqueFolder\myExeFile.exe"
EntryPoint="Windows.FullTrustApplication">
<desktop:StartupTask
TaskId="myAppTaskID"
Enabled="true"
DisplayName="MyAppDisplayName" />
</desktop:Extension>
</Extensions>

I have successfully added these references:

-Windows(Windows.winmd)
-System.Runtime.WindowsRuntime(WindowsRuntime.dll)
-Windows.ApplicationModel.StartupTaskContract
-Windows.Foundation.FoundationContract

I modified the reference for Windows(Windows.winmd) alias to be something else other than 'global' to avoid the compiler error:

StateError The type 'StartupTaskContract' exists in both '...' and '...'.

The following c# code is an attempt to get started with this functionality by trying to get the startuptaskId for the app which unfortunately gives me an error.

C# code:

async void MyFunct()
{
StartupTask startupTask = await StartupTask.GetAsync("myAppTaskID");
}

Compiler error I am currently receiving:

StateError CS1929 'IAsyncOperation<StartupTask>' does not contain a definition for 'GetAwaiter' and the best extension method overload 'WindowsRuntimeSystemExtensions.GetAwaiter(IAsyncAction)' requires a receiver of type 'IAsyncAction'

Lastly, this enablement and disablement functionality should be reflected in the Windows 10 Startup Apps window showing that the app is either ON or OFF.

I have tried multiple different things with no luck. Maybe I am overlooking some things so having an outside perspective would be helpful.

With the app manifest extension code, the app does boot up automatically at system startup.

I am aware that the users can just turn the app on or off via the startup app window or task manager. But this functionality is a requirement for a client.

Any suggestions would be much appreciated.

Thanks for reading~

someuser193
  • 55
  • 10

1 Answers1

1

packages

  <package id="Antlr" version="3.4.1.9004" targetFramework="net452" />
  <package id="bootstrap" version="3.0.0" targetFramework="net452" />
  <package id="jQuery" version="1.10.2" targetFramework="net452" />
  <package id="jQuery.Validation" version="1.11.1" targetFramework="net452" />
  <package id="Microsoft.AspNet.Identity.Core" version="2.2.1" targetFramework="net452" />
  <package id="Microsoft.AspNet.Mvc" version="5.2.3" targetFramework="net452" />
  <package id="Microsoft.AspNet.Razor" version="3.2.3" targetFramework="net452" />
  <package id="Microsoft.AspNet.Web.Optimization" version="1.1.3" targetFramework="net452" />
  <package id="Microsoft.AspNet.WebPages" version="3.2.3" targetFramework="net452" />
  <package id="Microsoft.CodeDom.Providers.DotNetCompilerPlatform" version="1.0.0" targetFramework="net452" />
  <package id="Microsoft.jQuery.Unobtrusive.Validation" version="3.2.3" targetFramework="net452" />
  <package id="Microsoft.Net.Compilers" version="1.0.0" targetFramework="net452" developmentDependency="true" />
  <package id="Microsoft.Rest.ClientRuntime" version="2.3.2" targetFramework="net452" />
  <package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net452" />
  <package id="Modernizr" version="2.6.2" targetFramework="net452" />
  <package id="Newtonsoft.Json" version="6.0.8" targetFramework="net452" />
  <package id="Respond" version="1.2.0" targetFramework="net452" />
  <package id="WebGrease" version="1.5.2" targetFramework="net452" />

usings

using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Rest;

method

void MyFunct()
{

   Task.Factory.StartNew(s => StartupTask.GetAsync("myAppTaskID"), this, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default).GetAwaiter();

// or 

   StartupTask.GetAsync("myAppTaskID").wait();
}
Mertuarez
  • 901
  • 7
  • 24
  • Thanks for your response. I tried your suggestion and I received the following error: Error CS1929 'Task>' does not contain a definition for 'Unwrap' and the best extension method overload 'TaskExtensions.Unwrap(Task)' requires a receiver of type 'Task'. Am I missing some dll or a using namespace? – someuser193 Apr 09 '20 at 08:21
  • I did some changes. – Mertuarez Apr 09 '20 at 09:31
  • Your help is much appreciated.This time I get no compiler errors when I compile the app but the StartupTask.GetAsync("myAppTaskID") portion of the StartNew method will not be run like this.I know this because if I try to simplify that statement to just this: var myVar = StartupTask.GetAsync("myAppTaskID"); and I try and access that simplified portion of code during runtime, I get a runtime error that crashes the app. The error says "System.Exception: Element not found. (Exception from HRESULT: 0x800...) at Windows.ApplicationModel.StartupTask.GetAsync(String taskId)".What do you make of this – someuser193 Apr 10 '20 at 07:28
  • This leads me to believe that the StartNew method is getting overlooked or never ran because it cannot access that element. Then when I try to access it directly with that simplified version, I get that runtime error that crashes the app. When you tried this on your end, were you able to successfully change the apps' startup settings with this code and have it be reflected in the Windows 10 Startup Apps window or task manager? If so, what am I not doing correctly? – someuser193 Apr 10 '20 at 07:34
  • Maybe your task is missing interface or something IBackgroundTask. – Mertuarez Apr 10 '20 at 12:33
  • Hi again,Sorry for the late reply.I've been doing some tinkering and I have figured out the problem.I believe your solution works but I was able to accomplish the same thing without using the StartNew method.The reason I was getting the element not found error was because I was accessing this line code StartupTask.GetAsync("myAppTaskID") from within an unpackaged winform app where that code should only reference the UWP appmanifest file code. After I packaged the app with desktop bridge and accessed that code as UWP app, it worked. Thanks for the help! – someuser193 Apr 14 '20 at 00:56
  • @someuser193 nice :D – Mertuarez May 29 '20 at 10:48
  • @someuser93. Are you sure you need all those packages to fix this particular issue? Which out of those do I need to get this to work. – Ste Nov 30 '21 at 12:10