C#Backgroundworker ConsoleWriteline(C# Backgroundworker ConsoleWriteline)
我有一个简单的Backgroundworker,想把我的结果写到控制台,我也想报告这个过程。
class Program { private static BackgroundWorker worker; static int counter; static void Main(string[] args) { worker = new BackgroundWorker(); worker.WorkerReportsProgress = true; worker.RunWorkerAsync(); Console.ReadLine(); } static void worker_DoWork(object sender, DoWorkEventArgs e) { while (true) { counter++; worker.ReportProgress(counter); } } static void worker_ProgressChanged(object sender, ProgressChangedEventArgs e) { Console.WriteLine(counter); } static void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { } }但是,如何将我的String(或多于1个字符串)提供给我的ReportProcess函数?
I have a simple Backgroundworker and want to write my result to the Console and I also want to report the process.
class Program { private static BackgroundWorker worker; static int counter; static void Main(string[] args) { worker = new BackgroundWorker(); worker.WorkerReportsProgress = true; worker.RunWorkerAsync(); Console.ReadLine(); } static void worker_DoWork(object sender, DoWorkEventArgs e) { while (true) { counter++; worker.ReportProgress(counter); } } static void worker_ProgressChanged(object sender, ProgressChangedEventArgs e) { Console.WriteLine(counter); } static void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { } }But how can I give my String (or more then 1 String) to my ReportProcess Function?
最满意答案
您没有将事件处理程序分配给BackgroundWorker事件:
static void Main(string[] args) { worker = new BackgroundWorker(); worker.DoWork += Worker_DoWork; //here worker.ProgressChanged += Worker_ProgressChanged; //and here worker.WorkerReportsProgress = true; worker.RunWorkerAsync(); Console.ReadLine(); }干杯
You're not assigning the event handlers to the BackgroundWorker events:
static void Main(string[] args) { worker = new BackgroundWorker(); worker.DoWork += Worker_DoWork; //here worker.ProgressChanged += Worker_ProgressChanged; //and here worker.WorkerReportsProgress = true; worker.RunWorkerAsync(); Console.ReadLine(); }Cheers
更多推荐
发布评论