c# - UWP 后台任务错误

标签 c# exception uwp background-task

我想创建一个在应用程序启动时启动的后台任务。为此,我使用应用程序触发器。

MainPage.xaml.cs

    var trigger = new ApplicationTrigger();
    BackgroundManagement.RegisterBackgroundTask("InternetBackgroundTask.InternetBackground", "Internet", trigger, null);
    await trigger.RequestAsync();

后台管理.cs

    public static BackgroundTaskRegistration RegisterBackgroundTask(string taskEntryPoint,string taskName,IBackgroundTrigger trigger,IBackgroundCondition condition)
    {
        //
        // Check for existing registrations of this background task.
        //

        foreach (var cur in BackgroundTaskRegistration.AllTasks)
        {

            if (cur.Value.Name == taskName)
            {
                //
                // The task is already registered.
                //

                return (BackgroundTaskRegistration)(cur.Value);
            }
        }

        //
        // Register the background task.
        //

        var builder = new BackgroundTaskBuilder();

        builder.Name = taskName;
        builder.TaskEntryPoint = taskEntryPoint;
        builder.SetTrigger(trigger);
        

        if (condition != null)
        {

            builder.AddCondition(condition);
        }

        BackgroundTaskRegistration task = builder.Register();
       
        return task;
    }

另一个项目上的Mytask

namespace InternetBackgroundTask
{
public sealed class InternetBackground : IBackgroundTask
{

    public void Run(IBackgroundTaskInstance taskInstance)
    {
        System.Diagnostics.Debug.WriteLine("Run Background Task");
    }
}

因此,当我启动我的应用程序时出现此错误:

Exception thrown at 0x776BE26B (KernelBase.dll) in backgroundTaskHost.exe: 0x04242420 (parameters: 0x31415927, 0x5DE30000, 0x003CED68).

Exception thrown at 0x776BE26B in backgroundTaskHost.exe: Microsoft C++ exception: EETypeLoadException at memory location 0x003CDF18.

Exception thrown at 0x776BE26B in backgroundTaskHost.exe: Microsoft C++ exception: [rethrow] at memory location 0x00000000.

Exception thrown at 0x776BE26B in backgroundTaskHost.exe: Microsoft C++ exception: EETypeLoadException at memory location 0x003CDF18.

我已经在我的项目中引用了我的后台任务,并且我已经在我的 list 中添加了我的后台任务

最佳答案

从您的截图中有两件事不明显:

您是否在 Package.appxmanifest 的“声明”下声明了您的后台任务?

第二个: “InternetBackground.cs”在什么项目类型中?它应该是一个 Windows 运行时组件

关于c# - UWP 后台任务错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38586958/

相关文章:

c# - 将字符串拆分为子字符串以节省大空间

c# - .NET/Silverlight/JavaScript/Flash 上的有向图布局引擎

c# - UWP 应用程序中的 Async Task.Delay 开销,是否有更好的解决方案?

c# - UWP 导航查看同一类型的多个页面

c# - Windows 应用商店验证订阅服务器端

c# - 存储 XML 中的元素

c# - 列表框项目中的第一个元素与第二个元素重叠

flutter - 方法 'findAncestorStateOfType' 在 flutter dart 中被 null 调用

python - 总是执行 finally block ,除了一个异常

java - try catch 异常时继续执行