У меня нет подробных сведений об управлении потоками в C#. Я хочу создать индикатор выполнения при запуске метода.Запуск панели выполнения с использованием фонового контроллера в C#
У меня есть две формы:
Form1
является форма, которая показывает, как вы запустите приложение. Он имеет кнопку под названиемbtnScrape
. Когда он нажимается, следует вызвать метод, должна появиться форма с индикатором выполнения.Form1
должен быть отключен для пользователя, пока индикатор выполнения не будет завершен.ProgressBarForm
- у этого есть индикатор выполнения и метка.
Код выглядит следующим образом.
//In Form1.cs I have a button.
private void btnScrape_Click(object sender, EventArgs e)
{
//gather data for folloeing parameters from the form.
Controller cntrlr = new Controller(urlFilePath, destinationPath, destinationfilename,cmbDepth.SelectedIndex);
cntrlr.Vmain(); // this is the method in controller class. while this is running i want show the progress bar.
}
// in Contrller class
class Controller{
List<string> urlList = null;
URLFileReader urlFileReader = null;
HTTPWorker httpWorker = null;
SourceReader srcreader = null;
ReportWriter reportWriter = null;
string urlFilePath, destinationPath, destinationFileName;
int depth;
public Controller(string urlFilePath,string destinationPath,string destinationFileName,int depth)
{
this.urlFilePath = urlFilePath;
this.destinationPath = destinationPath;
this.destinationFileName = destinationFileName;
this.urlList = new List<string>();
this.urlFileReader = new URLFileReader();
this.httpWorker = new HTTPWorker(this.destinationPath);
this.reportWriter = new ReportWriter(this.destinationPath,this.destinationFileName);
this.srcreader = new SourceReader(this.reportWriter);
this.depth = depth;
}
//this is the method
public void Vmain(){
this.urlFileReader.ReadURL(urlFilePath);
this.urlList = urlFileReader.geturlList();
string pageSrc;
foreach (string requestUrl in urlList)
{
//do sruff for requestUrl
//the progressbar should move as the urlList iterate.
//additionally i want the label on the top of progress bar to display the current "requestUrl"
//as the urlList is over i want quit from the progressbar window and come back to Form1. Till the progrss bar is completed Form1 should be disabled for the user.
}
}
}
Пожалуйста, объясните, что там происходит, и дайте рабочий код, если сможете. Заранее спасибо. Не было никакого идеального ответа, который работал на меня, хотя я потратил на это два дня. Я пробовал с BackgroundWorker
и потоками. Но решения не найдено. :(
спасибо. Конечно, это сработает. Но я хочу что-то другое. Я хочу увеличить индикатор выполнения, пока этот foreach работает в методе Vmain. Может, пожалуйста, скажите мне точное решение. Это поможет вам понять, что происходит. –
Я отредактировал свой ответ. Я изменил только основную форму, возможно, это поможет сейчас. – AstralisSomnium
Жаль, что я возвращаюсь четыре года спустя. Но это действительно помогло мне в то время. –