2012-07-16 18 views
6

मेरे पास एक साधारण ऐप है जो डेटा गहन कार्यों की श्रृंखला की आग लगाता है। मैं WinForms के साथ बहुत अनुभवी नहीं हूं और मैं इंटरफ़ेस को लॉक किए बिना ऐसा करने का सबसे अच्छा तरीका सोच रहा था। क्या पृष्ठभूमि वर्कर का पुन: उपयोग किया जा सकता है, या ऐसा करने का कोई और तरीका है?विनफॉर्म मल्टीथ्रेडिंग। BackgroundWorker का प्रयोग करें या नहीं?

धन्यवाद

+0

मैं BackgroundWorker का प्रयोग करेंगे। – Tomtom

+0

यदि आपको केवल एक अन्य धागे की आवश्यकता है तो मैं पृष्ठभूमि कार्यकर्ता का उपयोग करूंगा, वे उपयोग करने में बहुत आसान हैं और अधिकांश कड़ी मेहनत की गई है, जो आपको कार्य पूरा करने के विभिन्न चरणों के लिए सीधे आगे की घटनाओं के साथ प्रदान करती है। – ThePower

+0

पुन: उपयोग करके आपका क्या मतलब है? –

उत्तर

6

backgroundWorker इस्तेमाल किया जा सकता होगा।

अपने लाभ - यह आप एक प्रगति बार अद्यतन करें और यूआई नियंत्रण के साथ बातचीत करने के लिए अनुमति देता है। (WorkerReportsProgress)

इसके अलावा यह एक रद्द तंत्र है। (WorkerSupportsCancellation)

enter image description here

3

BackgroundWorker एक धागा है कि उन्हें सूचना तुल्यकालन शामिल है। उदाहरण के लिए, यदि आप स्कैन पूरा करते समय अपना यूआई अपडेट करना चाहते हैं, तो नियमित Thread यूआई ऑब्जेक्ट्स तक नहीं पहुंच सकता है (केवल यूआई थ्रेड ऐसा कर सकता है); इसलिए, BackgroundWorker एक पूर्ण ईवेंट हैंडलर प्रदान करता है जो ऑपरेशन पूर्ण होने पर यूआई थ्रेड पर चलता है।

अधिक जानकारी के लिए

देखें: Walkthrough: Multithreading with the BackgroundWorker Component(MSDN)

और एक साधारण नमूना कोड:

var worker = new System.ComponentModel.BackgroundWorker(); 
worker.DoWork += (sender,e) => Thread.Sleep(60000); 
worker.RunWorkerCompleted += (sender,e) => MessageBox.Show("Hello there!"); 
worker.RunWorkerAsync(); 
2

आप इस तरह की आवश्यकताओं के लिए BackgroundWorker का उपयोग कर सकते हैं। नीचे एक नमूना है जो updates a label status based on percentage task [long running] completion है। साथ ही, एक नमूना व्यवसाय वर्ग है जो कुछ मान निर्धारित करता है और मान ProgressChanged हैंडलर के माध्यम से यूआई पर वापस सेट किया जाता है। DoWork वह जगह है जहां आप अपना लंबा चलने वाला कार्य तर्क लिखते हैं। एक Winforms ऐप & पर एक लेबल और पृष्ठभूमि कार्यकर्ता घटक जोड़ने के बाद नीचे दिए गए कोड को कॉपी-पेस्ट करें, इसे एक शॉट दें। आप विभिन्न हैंडलर [RunWorkerCompleted, ProgressChanged, DoWork] पर डीबग कर सकते हैं और InitWorker विधि पर एक नज़र डालें। cancellation feature पर भी ध्यान दें।

using System.ComponentModel; 
using System.Windows.Forms; 

namespace WindowsFormsApplication1 
{ 
    public partial class Form3 : Form 
    { 
     private BackgroundWorker _worker; 
     BusinessClass _biz = new BusinessClass(); 
     public Form3() 
     { 
      InitializeComponent(); 
      InitWorker(); 
     } 

     private void InitWorker() 
     { 
      if (_worker != null) 
      { 
       _worker.Dispose(); 
      } 

      _worker = new BackgroundWorker 
      { 
       WorkerReportsProgress = true, 
       WorkerSupportsCancellation = true 
      }; 
      _worker.DoWork += DoWork; 
      _worker.RunWorkerCompleted += RunWorkerCompleted; 
      _worker.ProgressChanged += ProgressChanged; 
      _worker.RunWorkerAsync(); 
     } 


     void DoWork(object sender, DoWorkEventArgs e) 
     { 
      int highestPercentageReached = 0; 
      if (_worker.CancellationPending) 
      { 
       e.Cancel = true; 
      } 
      else 
      { 
       double i = 0.0d; 
       int junk = 0; 
       for (i = 0; i <= 199990000; i++) 
       { 
        int result = _biz.MyFunction(junk); 
        junk++; 

        // Report progress as a percentage of the total task. 
        var percentComplete = (int)(i/199990000 * 100); 
        if (percentComplete > highestPercentageReached) 
        { 
         highestPercentageReached = percentComplete; 
         // note I can pass the business class result also and display the same in the LABEL 
         _worker.ReportProgress(percentComplete, result); 
         _worker.CancelAsync(); 
        } 
       } 

      } 
     } 

     void RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) 
     { 
      if (e.Cancelled) 
      { 
       // Display some message to the user that task has been 
       // cancelled 
      } 
      else if (e.Error != null) 
      { 
       // Do something with the error 
      } 
     } 

     void ProgressChanged(object sender, ProgressChangedEventArgs e) 
     { 
      label1.Text = string.Format("Result {0}: Percent {1}",e.UserState, e.ProgressPercentage); 
     } 
    } 

    public class BusinessClass 
    { 
     public int MyFunction(int input) 
     { 
      return input+10; 
     } 
    } 
} 
+0

धन्यवाद। मेरी चिंता DoWork घटना में अपना "लंबा चलने वाला कार्य तर्क" जोड़ने के साथ है।मेरे पास कई कार्य हैं जिन्हें चलाने की आवश्यकता है और यह उन्हें एक DoWork ईवेंट में जोड़ने के लिए व्यावहारिक प्रतीत नहीं होता है। – Brian

संबंधित मुद्दे