2015-09-27 4 views
7

अगर फोरग्राउंड ऐप निष्पादित किया जा रहा है तो मैं पृष्ठभूमि कार्य को कैसे रोक सकता हूं? मेरे ऐप के लिए यूनिवर्सल विंडोज प्लेटफ़ॉर्म का उपयोग करना।विंडोज 10 यूडब्लूपी - अगर फोरग्राउंड ऐप चल रहा है तो पृष्ठभूमि कार्य बंद करें

मेरा पृष्ठभूमि कार्य कुछ साइट पर नए आइटम की जांच कर रहा है और नई चीजें उपलब्ध होने पर टोस्ट भेजती है, लेकिन अगर उपयोगकर्ता अभी आवेदन कर रहा है तो टोस्ट भेजने से रोकना नहीं है।

मैंने अपने एप्लिकेशन लॉन्च पर कार्य को अनधिकृत करने का प्रयास किया है और ऐप समाप्त होने पर इसे फिर से पंजीकृत करने के लिए, यह मेरे लिए अच्छा समाधान नहीं है - जब डिवाइस किसी कारण से बंद हो जाता है (उदाहरण के लिए, बैटरी हटा दी गई थी) ऐप लॉन्च होने तक कार्य पंजीकृत नहीं होगा।

धन्यवाद।

+1

क्या आपको कोई समाधान मिला? क्या आप एप्लिकेशन ट्रिगर कार्य का उपयोग कर रहे हैं? – peterincumbria

उत्तर

0

मुझे एक आसान समाधान देखने में बहुत दिलचस्पी होगी। इसे छोड़कर, आप एक मध्यस्थता तंत्र डाल सकते हैं। इन पंक्तियों के साथ कुछ:

public class AppSynchronization 
{ 
    // Called by the main app on launch or on resume. It will signal the main app's intention to start. 
    // The main app will not perform any significant actions before this method returns. 
    public void ActivateMainApp() {...} 

    // Called by the main app. It will signal the fact that the main app is "going away". 
    public async Task MainAppSuspended() {...} 

    // Called by the background agent. 
    // It will signal the background agent intention to start. 
    // This method will only return if the main app is out of the way. 
    // It will return a cancellation token that will be used to cancel the activity of the background agent when the main app advertises its intention to start. 
    public async Task<CancellationToken> ActivateBackgroundAgent(CancellationToken cancelWait) 
    { 
     // Make sure the main app is not started or wait until the main app is out of the way 

     // Start a thread that is on the lookout for the main app announcing that it wants to start. 
     // When that happens it will cancel the cancellation token returned. 
    } 

    // <summary> 
    // Called by the background agent. 
    // It will signal the fact that the background agent completed its actions. 
    public async Task DeactivateBackgroundAgent() 
} 

मुख्य ऐप्लिकेशन में:

private AppSynchronization appSynchronization; 

public App() 
{ 
    ... 
    this.appSynchronization = new AppSynchronization(); 
} 

protected async override void OnLaunched(LaunchActivatedEventArgs e) 
{ 
    ... 
    if (rootFrame.Content == null) 
    { 
     // Advertise the fact that the main app wants to start. 
     // The background agent will know to cancel whatever its doing. 
     // ActivateMainApp may have to be async although you need to make sure that OnLaunched supports that 
     this.appSynchronization.ActivateMainApp(); 
     ... 
    } 
} 

private async void OnResuming(object sender, object e) 
{ 
    ... 
    // Advertise the fact that the main app wants to resume. 
    // The background agent will know to cancel whatever its doing. 
    this.appSynchronization.ActivateMainApp(); 
} 


private async void OnSuspending(object sender, SuspendingEventArgs e) 
{ 
    var deferral = e.SuspendingOperation.GetDeferral(); 

    ... 
    // Advertise the fact that the main app is suspending. 
    // The background agent will know it is allowed to start doing work. 
    await _mainAppSynchronization.MainAppSuspended(); 

    ... 
    deferral.Complete(); 
} 

private void OnUnhandledException(object sender, UnhandledExceptionEventArgs e) 
{ 
    ... 
    // Advertise the fact that the main app is going away. 
    // The background agent will know it is allowed to start doing work. 
    _mainAppSynchronization.MainAppSuspended().Wait(); 
} 

और पृष्ठभूमि एजेंट में:

public sealed class BackgroundTask : IBackgroundTask 
{ 
    public async void Run(IBackgroundTaskInstance taskInstance) 
    { 
     ... 
     AppSynchronization appSynchronization = new AppSynchronization(); 
     BackgroundTaskDeferral deferral = taskInstance.GetDeferral(); 

     // Make sure that the main app is not started. If it is started then wait until the main app gets out of the way. 
     // It he main app is running this will wait indefinitely. 
     // Use backgroundAgentCancellationToken to cancel the actions of the background agent when the main app advertises its intention to start. 
     CancellationToken backgroundAgentCancellationToken = await appSynchronization.ActivateBackgroundAgent(); 

     await DoBackgroundAgentWork(backgroundAgentCancellationToken) 

     // Advertise the fact that the background agent is out. 
     // DeactivateBackgroundAgent will make sure that the synchronization mechanism advertised the fact that the background agent is out. 
     // DeactivateBackgroundAgent may have to be declared async in case the synchronization mechanism uses async code to do what is needed. 
     await appSynchronization.DeactivateBackgroundAgent(); 

     deferral.Complete(); 
    } 

मैं प्रक्रियाओं में संवाद करने के लिए किसी भी तरह से वहाँ है अगर यकीन नहीं है यूडब्ल्यूपी में मध्यस्थता तंत्र को स्थानीय भंडारण पर फ़ाइलों पर आधारित होना पड़ सकता है।

मध्यस्थता तंत्र को इस घटना में दिल की धड़कन तंत्र शामिल करना पड़ सकता है कि एक या दूसरी प्रक्रिया एक विनाशकारी तरीके से दुर्घटनाग्रस्त हो जाती है।

+0

"यदि मुख्य ऐप चल रहा है तो यह अनिश्चित काल तक प्रतीक्षा करेगा।" (बीजी कार्य में)। क्या तुम इसके बारे में निश्चित हो? क्या वे बहुत लंबे समय तक चलने पर पृष्ठभूमि कार्यों को मार नहीं सकते हैं? या इंतजार सीपीयू उपयोग के रूप में नहीं गिना जाता है? यदि संभव हो तो कुछ अंतर्दृष्टि प्राप्त करना अच्छा लगेगा। धन्यवाद :) – sibbl

+0

यदि सक्रिय बैकग्राउंडएजेंट में लागू मध्यस्थता तंत्र में एक लूप होता है जहां यह कुछ सेकंड सोता है तो मुख्य ऐप की स्थिति की जांच करता है (शायद मुख्य ऐप बना रहे फ़ाइल की उपस्थिति की जांच करके) तो CPU उपयोग होगा बहुत छोटा। नींद का समय गिना नहीं जाएगा। आखिरकार पृष्ठभूमि एजेंट को निलंबित कर दिया जाएगा और फिर आपको ओएस के लिए फिर से शुरू करने के लिए कुछ समय इंतजार करना होगा। हालांकि आपको इसके आसपास बहुत सारे परीक्षण करना होगा। जहां तक ​​मुझे पता है कि विंडोज 8.1 बनाम WP 8.1 में पृष्ठभूमि एजेंट महत्वपूर्ण अंतर था। यूडब्ल्यूपी के बारे में निश्चित नहीं है। – Ladi

+0

यूडब्ल्यूपी में यदि डिवाइस यूसे ट्रिगर ट्रिगर नहीं है, तो पृष्ठभूमि कार्य किसी एप्लिकेशन ट्रिगर के मामले में 10 मिनट के बाद रद्द हो जाएगा। इससे मुझे एक जीपीएस ट्रैकिंग ऐप के साथ वास्तविक दर्द हो रहा है क्योंकि सेंसरकोर अब हटा दिया गया है। – peterincumbria

1

अग्रभूमि एप्लिकेशन और पृष्ठभूमि एजेंट

0

मैं backgroundtask जो TimeTrigger से शुरू हो रहा है के बीच सिंक्रनाइज़ करने के लिए एक नामित Mutex का प्रयोग करें। ऐप लोड करने की ज़रूरत के बिना फोन को फिर से शुरू करने के बाद यह कार्य स्वचालित रूप से पुन: प्रारंभ होता है। हो सकता है कि मदद मिलेगी ...

[संपादित] सिर्फ एहसास हुआ मैं अपने जवाब के साथ देर से आने हूँ ...

0

आप नीचे दिए गए कोड का उपयोग कर अनुप्रयोग की स्थिति देख सकते

var value = ApplicationSettingsHelper.ReadResetSettingsValue(ApplicationSettingsConstants.AppState); 
if (value == null) 
    foregroundAppState = AppState.Unknown; 
else 
    foregroundAppState = EnumHelper.Parse<AppState>(value.ToString()); 

if (foregroundAppState == AppState.Suspended) 
     //Do something 
else if (foregroundAppState == AppState.Active) 
     return; 
else if (foregroundAppState == AppState.Unknown) 
     return; 
0

ऐसा करने का तरीका एप्लिकेशन स्थानीय सेटिंग्स का उपयोग करना है।

अग्रभूमि और पृष्ठभूमि में सेटिंग सेट करें। अग्रभूमि में एक उदाहरण के रूप में ऐसा करते हैं।

/// <summary> 
/// When the window visibility changes 
/// </summary> 
/// <param name="sender">object sender</param> 
/// <param name="e">VisibilityChangedEventArgs e</param> 
private void OnVisibilityChanged(object sender, VisibilityChangedEventArgs e) 
{ 
    var localSettings = ApplicationData.Current.LocalSettings; 

    if (!e.Visible) 
    { 
     localSettings.Values["AppInForeground"] = false;    
    } 
    else 
    { 
     localSettings.Values["AppInForeground"] = true; 
    } 
} 

दुर्भाग्य sibbi अगर आप DeviceUseTrigger अपनी पृष्ठभूमि कार्य के लिए 10 मिनट के बाद रद्द हो जाएगा का उपयोग नहीं कर रहे हैं, सही है।

0

यह इस सवाल के समान है: How to stop the background task if the app is starting?

सबसे आसान तरीका में proc पृष्ठभूमि कार्य करने के लिए अपने कार्यों को परिवर्तित करने के लिए किया जाएगा: एक में proc कार्य के साथ https://docs.microsoft.com/en-us/windows/uwp/launch-resume/convert-out-of-process-background-task

आप जाँच कर सकते हैं अनुप्रयोग है कि क्या अग्रभूमि में या पृष्ठभूमि गतिविधि से सीधे नहीं।

आप अपने मुख्य अनुप्रयोग से अलग प्रक्रियाओं के रूप में अपने पृष्ठभूमि कार्य रखने की जरूरत है, तो आप निम्न में से एक कर सकते हैं:

  • एक फ़ाइल के माध्यम से एक Mutex बनाएँ।
  • एक इन-प्रोप ऐप सेवा बनाएं और अपने पृष्ठभूमि कार्य से संवाद करने का प्रयास करें। फिर आप एक मूल्य वापस कर सकते हैं कि ऐप अग्रभूमि में है या नहीं।
  • फोरग्राउंड ऐप चल रहा है या नहीं, इसके बारे में ध्वज पकड़ने के लिए ऐप स्थानीय सेटिंग्स शब्दकोश का उपयोग करें। यह सबसे नाजुक है, क्योंकि एक ऐप क्रैश ध्वज को सही ढंग से रीसेट नहीं कर सकता है।
संबंधित मुद्दे