खोज…


टिप्पणियों

  • एक अलग प्रक्रिया में चलने वाले पृष्ठभूमि कार्य को पंजीकृत करने के लिए, आपको Package.appxmanifest में "घोषणाएँ" टैब पर जाना होगा और एक नया "बैकग्राउंड टास्क" जोड़ना होगा और प्रवेश बिंदु निर्धारित करना होगा।
  • एक एकल-प्रक्रिया पृष्ठभूमि कार्य को पंजीकृत करना BackgroundTaskBuilder माध्यम से किया जा सकता है, लेकिन यदि आप किसी कार्य को दो बार पंजीकृत करते हैं, तो एप्लिकेशन अपवाद छोड़ देगा, इसलिए आपको यह देखना होगा कि क्या आपने पहले ही कोई कार्य पंजीकृत किया है।
  • नए कार्य को पंजीकृत करने के लिए ऐप को अधिकार प्राप्त करना चाहिए, यह BackgroundExecutionManager.RequestAccessAsync() को कॉल करके किया जा सकता है, लेकिन सुनिश्चित करें कि आपके पास वास्तव में अनुमति है। कॉल एक्सेस का प्रकार लौटाता है ( BackgroundAccessStatus enum) जो यह इंगित करेगा कि आपके पास एक्सेस है या नहीं।
  • जब तक पैकेज की स्थापना रद्द नहीं हो जाती, तब तक पंजीकृत कार्यों को रखा जाता है, लेकिन हर लॉन्च पर आपके द्वारा आवश्यक कार्यों की जांच करने के लिए यह चोट नहीं पहुंचेगी, बग होता है!
  • जब आवेदन अद्यतन किया जाता है, तो एक नया कार्य पंजीकृत करने की अनुमति रद्द कर दी जाती है। अपडेट के बाद अपने ऐप को चालू रखने के लिए, खासकर यदि आपने एक नया टास्क रजिस्टर जोड़ा है, तो आपको BackgroundAccessManager माध्यम से एक्सेस ओवर को हटाना और अनुरोध करना होगा । यह जानने का एक तरीका है कि आपका ऐप अपडेट है या नहीं, किसी अन्य कार्य को SystemTrigger , SystemTrigger के प्रकार के साथ पंजीकृत SystemTriggerType.ServicingComplete

एक कार्य पंजीकृत करना

/// <summary>
/// Registers a background task in the system waiting to trigger
/// </summary>
/// <param name="taskName">Name of the task. Has to be unique</param>
/// <param name="taskEntryPoint">Entry point (Namespace) of the class (has to implement IBackgroundTask and has to be in a Windows Runtime Component) to start</param>
/// <param name="trigger">What has to be triggered to start the task</param>
/// <param name="condition">Optional condition. Can be null</param>
/// <param name="recreateIfExists">Should the Task be recreated if it already exists?</param>
/// <returns></returns>
public BackgroundTaskRegistration RegisterTask(string taskName, string taskEntryPoint, IBackgroundTrigger trigger, IBackgroundCondition condition = null) {
    Debug.WriteLine("Try registering task: " + taskName);

    var builder = new BackgroundTaskBuilder {
        Name = taskName,
        TaskEntryPoint = taskEntryPoint
    };

    builder.SetTrigger(trigger);

    if (condition != null) {
        builder.AddCondition(condition);
    }

    try {
        var task = builder.Register();
        Debug.WriteLine("Task successfully registered");
        return task;
    } catch (Exception exception) {
        Debug.WriteLine("Error creating Task: " + exception);
        return null;
    }
}

इसके नाम से पंजीकृत कार्य प्राप्त करें

/// <summary>
/// Gets a BackgroundTask by its name
/// </summary>
/// <param name="taskName">Name of the task to find</param>
/// <returns>The found Task or null if none found</returns>
public BackgroundTaskRegistration TaskByName(string taskName) =>
       BackgroundTaskRegistration.AllTasks.FirstOrDefault(x => x.Value.Name.Equals(taskName)).Value as BackgroundTaskRegistration;

काम

public sealed class BackgroundTask : IBackgroundTask {

    private BackgroundTaskDeferral _deferral;

    /// <summary>
    /// Registers the listener to check if the button is pressed
    /// </summary>
    /// <param name="taskInstance">An interface to an instance of the background task. The system creates this instance when the task has been triggered to run.</param>
    public async void Run(IBackgroundTaskInstance taskInstance) {
        _deferral = taskInstance.GetDeferral();

        //Do async operations here

        _deferral.Complete();
    }
}

जाँच करें कि क्या कार्य पंजीकृत है

private bool IsTaskRegistered(string taskName) =>
        BackgroundTaskRegistration.AllTasks.Any(x => x.Value.Name.Equals(taskName));

किसी कार्य को मैन्युअल रूप से ट्रिगर करना

var trigger = new ApplicationTrigger();
TaskHandlerMentionedInThisTutorial.RegisterTask(TaskName, entryPoint, trigger, null, true);    
await trigger.RequestAsync();

किसी कार्य को अनजाने में करना

/// <summary>
/// Unregister a single background task with given name
/// </summary>
/// <param name="taskName">task name</param>
/// <param name="cancelTask">true if task should be cancelled, false if allowed to finish</param>
public void UnregisterTask(string taskName, bool cancelTask) =>
    BackgroundTaskRegistration.AllTasks.First(x => x.Value.Name.Equals(taskName)).Value?.Unregister(cancelTask);

/// <summary>
/// Unregister an active group of background tasks, which name contains given string
/// </summary>
/// <param name="taskNamePart">part of the task name</param>
/// <param name="cancelTask">true if tasks should be cancelled, false if allowed to finish</param>
public void UnregisterTasks(string taskNamePart, bool cancelTask)
{
    foreach (var task in BackgroundTaskRegistration.AllTasks.Where(x => x.Value.Name.Contains(taskNamePart)))
        task.Value.Unregister(cancelTask);
}

ट्रिगर के साथ पृष्ठभूमि कार्य पंजीकृत करें

बैकग्राउंड टास्क कुछ अच्छा काम करने का तरीका है जबकि आपका एप्लिकेशन नहीं चल रहा है। तब उपयोग करने में सक्षम होने से पहले, आपको उन्हें पंजीकृत करना होगा।

यहां एक पृष्ठभूमि टास्क क्लास का एक नमूना है जिसमें एक ट्रिगर और एक शर्त के साथ पंजीकरण और रन कार्यान्वयन शामिल है

public sealed class Agent : IBackgroundTask
{    
    public void Run(IBackgroundTaskInstance taskInstance)
    {
        // run the background task code
    }
    
    // call it when your application will start.
    // it will register the task if not already done
    private static IBackgroundTaskRegistration Register()
    {
        // get the entry point of the task. I'm reusing this as the task name in order to get an unique name
        var taskEntryPoint   = typeof(Agent).FullName;
        var taskName         = taskEntryPoint;
        
        // if the task is already registered, there is no need to register it again
        var registration            = BackgroundTaskRegistration.AllTasks.Select(x => x.Value).FirstOrDefault(x => x.Name == taskName);
        if(registration != null) return registration;
                        
        // register the task to run every 30 minutes if an internet connection is available
        var taskBuilder             = new BackgroundTaskBuilder();
        taskBuilder.Name            = taskName;
        taskBuilder.TaskEntryPoint  = taskEntryPoint;
        taskBuilder.SetTrigger(new TimeTrigger(30, false));
        taskBuilder.AddCondition(new SystemCondition(SystemConditionType.InternetAvailable));
        
        return taskBuilder.Register();
    }
}


Modified text is an extract of the original Stack Overflow Documentation
के तहत लाइसेंस प्राप्त है CC BY-SA 3.0
से संबद्ध नहीं है Stack Overflow