मुझे इस तरह की समस्या है:सी # विनफॉर्म प्रोग्रेसबार और पृष्ठभूमिवर्कर
मेरे पास मेनफॉर्म नामक एक फॉर्म है। मेरे पास इस फॉर्म पर होने वाला एक लंबा ऑपरेशन है।
हालांकि यह लंबा ऑपरेशन चल रहा है, मुझे मेनफॉर्म के शीर्ष पर नाम प्रगतिफॉर्म से दूसरा दिखाना होगा।
प्रोग्रेसफॉर्म में प्रगति पट्टी शामिल है। लंबे ऑपरेशन के दौरान अपडेट होने की जरूरत है।
लंबे ऑपरेशन पूरा होने के बाद, प्रोग्रेसफॉर्म स्वचालित रूप से बंद होना चाहिए।
मैं निम्नलिखित की तरह कुछ कोड लिखा है:
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace ClassLibrary
{
public class MyClass
{
public static string LongOperation()
{
Thread.Sleep(new TimeSpan(0,0,30));
return "HelloWorld";
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace BackgroungWorker__HelloWorld
{
public partial class ProgressForm : Form
{
public ProgressForm()
{
InitializeComponent();
}
public ProgressBar ProgressBar
{
get { return this.progressBar1; }
set { this.progressBar1 = value; }
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ClassLibrary;
namespace BackgroungWorker__HelloWorld
{
public partial class MainForm : Form
{
ProgressForm f = new ProgressForm();
public MainForm()
{
InitializeComponent();
}
int count = 0;
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
if (f != null)
{
f.ProgressBar.Value = e.ProgressPercentage;
}
++count;
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (e.Cancelled)
{
MessageBox.Show("The task has been cancelled");
}
else if (e.Error != null)
{
MessageBox.Show("Error. Details: " + (e.Error as Exception).ToString());
}
else
{
MessageBox.Show("The task has been completed. Results: " + e.Result.ToString());
}
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
if (f == null)
{
f = new ProgressForm();
}
f.ShowDialog();
//backgroundWorker1.ReportProgress(100);
MyClass.LongOperation();
f.Close();
}
private void btnStart_Click(object sender, EventArgs e)
{
backgroundWorker1.RunWorkerAsync();
}
private void btnCancel_Click(object sender, EventArgs e)
{
backgroundWorker1.CancelAsync();
this.Close();
}
}
}
मैं Progressbar अद्यतन करने के लिए जिस तरह से नहीं मिल रहा हूँ।
मुझे backgroundWorker1.ReportProgress()
कहां रखना चाहिए और मुझे इसे कैसे कॉल करना चाहिए?
मुझे MyClass में कोई भी बदलाव नहीं करना चाहिए। Coz, मुझे नहीं पता कि क्या होगा या मेरे आवेदन की इस परत में ऑपरेशन को पूरा करने में कितना समय लगेगा।
क्या कोई मेरी मदद कर सकता है?
मुझे MyClass में कोई भी बदलाव नहीं करना चाहिए। Coz, मुझे नहीं पता कि मेरे ऐप की इस परत में क्या होगा। – anonymous
फिर आप प्रगति की सटीक रिपोर्ट नहीं कर सकते हैं। मेरा मतलब है कि आप प्रत्येक प्रगति पट्टी को हर सेकेंड में बढ़ाने के लिए 'प्रोग्रेसफॉर्म' में टाइमर जोड़ सकते हैं, लेकिन यह केवल भ्रम प्रदान करेगा। यदि आपके पास * वास्तविक * प्रगति का पता लगाने का कोई तरीका नहीं है, तो प्रगति पट्टी कितनी उपयोगी है? –