प्रगति पट्टी के साथ async में FsXaml आवेदन

0

सवाल

में एफ# (FsXaml/पीछे कोड) आवेदन, मैं का उपयोग करने के लिए एक प्रगति पट्टी के बिना उपयोग के लिए एक पृष्ठभूमि के कार्यकर्ता के रूप में मैं सी# में. के आधार पर इंटरनेट पर एक लेख में (लिंक यहाँ), मैं करने की कोशिश की, का उपयोग अतुल्यकालिक वर्कफ़्लोज़.

मैं बनाया कोड के आधार (कुछ विस्तार करने के लिए) पर उदाहरण में ऊपर उल्लिखित लेख है, लेकिन यह काम नहीं किया था के रूप में मैं उम्मीद थी. वर्तमान धागा (यूआई धागा) अभी भी अवरुद्ध है के रूप में अगर कोई async कोड वहां गया था. कोई स्विचन के लिए एक पृष्ठभूमि धागा होता है । प्रगति पट्टी को सक्रिय होता है के बाद ही लंबे समय से चल रहे आपरेशन समाप्त हो गया है । हटाने के onThreadPool समारोह में कोई प्रभाव नहीं है ।

मेरा सवाल है: क्या गलत है में कोड और कैसे यह सही बनाने के लिए?

type MainWindowXaml = FsXaml.XAML<"XAMLAndCodeBehind/MainWindow.xaml">

type MainWindow() as this =

    inherit MainWindowXaml()

    //....some code....

    let ButtonClick _ = 
   
        //....some code....
       
        let longRunningOperation() = //....some long running operation (reading from Google Sheets)....            
             
        let progressBar() = this.ProgressBar.IsIndeterminate <- true     

        let doBusyAsync progress operation =  
            progress
            async
                {   
                  do! operation
                }
            |> Async.StartImmediate 
    
        let onThreadPool operation =
            async
                {    
                  let context = System.Threading.SynchronizationContext.Current
                  do! Async.SwitchToThreadPool()
                  let! result = operation
                  do! Async.SwitchToContext context
                  return result
                } 
    
        let asyncOperation progress operation =   
            async { operation } 
            |> onThreadPool
            |> doBusyAsync progress 
    
        (progressBar(), longRunningOperation()) ||> asyncOperation 
      
    do
        //....some code....
        this.Button.Click.Add ButtonClick
asynchronous f# fsxaml
2021-11-23 23:13:28
2

सबसे अच्छा जवाब

3

वहाँ रहे हैं चीजों की एक संख्या के साथ गलत अपने कोड.

  • पहली, में progressBar(), longRunningOperation() आप वास्तव में फोन के लंबे समय से चल रहे ऑपरेशन और इसलिए यह सब हो जाता है चलाने के लिए यहाँ है । (के रूप में दूर के रूप में मैं कर सकते हैं लगता है अपने अधूरा नमूना है, यह सिर्फ एक समारोह कॉल, नहीं एक और अतुल्यकालिक आपरेशन) ।

  • आप तो पास के परिणाम operation और progress के आसपास है, लेकिन उन सिर्फ unit मान नहीं है कि वास्तव में कुछ भी नहीं है ।

  • नतीजतन, अतुल्यकालिक आपरेशन async { operation } है कि आप पारित करने के लिए onThreadPool कुछ भी नहीं है सब पर.

  • में doBusyAsync, आप का उपयोग Async.StartImmediate आपरेशन चलाने के लिए एक रास्ता अवरुद्ध (तो इस ब्लॉक के लिए होगा धागा, यहां तक कि अगर यह चल रहा था, कुछ वास्तविक ऑपरेशन).

  • एक तरफ किया जा रहा से अवरुद्ध है, तुम भी की जरूरत नहीं है async { do! operation } क्योंकि यह करने के लिए बराबर है बस operation.

सारांश में, अपने कोड के किसी भी तरह मिल गया जिस तरह से बहुत जटिल है. आप सरल होना चाहिए यह करने के लिए कुछ बहुत ही बुनियादी है के रूप में पहला कदम है । मैं नहीं है सही सेटअप करने के लिए यह कोशिश है, लेकिन मुझे लगता है कि निम्नलिखित की तरह कुछ चाल करना चाहिए:

let ButtonClick _ = 
  let longRunningOperation() = 
    // some long-running operation

  let asyncOperation() = async {
    // Start the progress bar here
    let context = System.Threading.SynchronizationContext.Current
    do! Async.SwitchToThreadPool()
    let result = longRunningOperation()
    do! Async.SwitchToContext context
    // Display the 'result' in your user interface
    // Stop the progress bar here
  }

  Async.Start(asyncOperation)

मैं सभी को हटा बेकार कार्यों और पैरामीटर गुजर रहा है और सरलीकृत के रूप में ज्यादा के रूप में संभव है - यह सिर्फ अपने लंबे समय से चल रहे ऑपरेशन कहा जाता है, जो सीधे से async एक बार जब यह स्विच करने के लिए धागा पूल. आप अपने परिणाम पाने के लिए और स्विच करने के बाद, वापस मूल करने के लिए संदर्भ के लिए सक्षम होना चाहिए कि प्रदर्शन में अपने उपयोगकर्ता इंटरफ़ेस. आदर्श रूप में, आप बनाना चाहते हैं longRunningOperation खुद अतुल्यकालिक (और यह फोन का उपयोग let!) लेकिन ऊपर काम करना चाहिए ।

2021-11-24 00:15:43
0

योग करने के लिए चीजों को, मैं बढ़ाया है Tomáš Petříček के कोड के साथ कोड से संबंधित के लिए लंबे समय से चल रहे ऑपरेशन पर आधारित जिम Foye की टिप्पणी के बारे में (hopping वापस यूआई धागे पर). अब कोड की तरह काम करता है एक आकर्षण है । मेरा धन्यवाद करने के लिए Tomáš Petříček के लिए उसकी तरह और विस्तृत जवाब.

    let low = string (this.TextBox2.Text)
    let high = string (this.TextBox3.Text)
    let path = string (this.TextBox4.Text)

    (* longRunningOperation() replaced by textBoxString4() and textBoxString3() 
       based on the comment by Jim Foye
    
    let longRunningOperation() = 
        async
            {
              match textBoxString4 low high >= 0 with
              | false -> this.TextBox1.Text <- textBoxString3 low high path 
              | true  -> this.TextBox1.Text <- "Chybný rozdíl krajních hodnot"        
            }
    *)

    let textBoxString4() = 
        async
            {
              let result = textBoxString4 low high
              return result
            }                  
                           
    let textBoxString3() =        
        async
            {
              //the actual long running operation (reading data 
              //from Google Sheets)
              let result = textBoxString3 low high path 
              return result
            }  

    let asyncOperation() = 
        async
            {
              let context = System.Threading.SynchronizationContext.Current
              this.ProgressBar2.IsIndeterminate <- true
              do! Async.SwitchToThreadPool()
              (*let! result = longRunningOperation() throws an exception 
              "The calling thread cannot access this object because
               a different thread owns it." 
              *)
              let! result4 = textBoxString4()  
              let! result3 = textBoxString3()  
              do! Async.SwitchToContext context
              match result4 >= 0 with
              | false -> this.TextBox1.Text <- result3
              | true  -> this.TextBox1.Text <- "Chybný rozdíl krajních hodnot" 
              this.ProgressBar2.IsIndeterminate <- false
            } 
    Async.StartImmediate(asyncOperation())//not working with Async.Start
                                             
2021-11-24 18:29:36

अन्य भाषाओं में

यह पृष्ठ अन्य भाषाओं में है

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................

इस श्रेणी में लोकप्रिय

लोकप्रिय सवाल इस श्रेणी में