C#Backgroundworker ConsoleWriteline(C# Backgroundworker ConsoleWriteline)

编程入门 行业动态 更新时间:2024-10-24 06:33:12
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

更多推荐

本文发布于:2023-07-04 10:56:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1023537.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Backgroundworker   ConsoleWriteline

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!