Forum: C# |
Thema:
AW: BackgroundWorker |
Von:
Christian Hehtke (
23.06.2006 23:28) |
hier mal einige variationen wie man das lösen kann. du kannst auch noch mehr lösungsansätze aber diese sollten reichen um dein problem zu lösen.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Threading;
namespace BW
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
// backgroundworker
BackgroundWorker bw = new BackgroundWorker();
bw.DoWork += new DoWorkEventHandler(bw_DoWork);
bw.ProgressChanged += new ProgressChangedEventHandler(bw_ProgressChanged);
bw.WorkerReportsProgress = true;
bw.RunWorkerAsync();
// own event
LongRunningTaskClass ltrc = new LongRunningTaskClass();
ltrc.Progress += new LongRunningTaskClass.ProgressEventDelegate(ltrc_Progress);
Thread t = new Thread(new ThreadStart(ltrc.LongRunningTask));
t.Start();
}
void ltrc_Progress(object sender, int percentage)
{
Debug.WriteLine("Own Event: " + percentage.ToString());
}
void bw_DoWork(object sender, DoWorkEventArgs e)
{
LongRunningTaskClass lrtc = new LongRunningTaskClass();
lrtc.LongRunningTask((BackgroundWorker)sender);
}
void bw_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
Debug.WriteLine("Backgroundworker: " + e.ProgressPercentage.ToString());
}
}
class LongRunningTaskClass
{
#region " Eventmodel "
public delegate void ProgressEventDelegate(object sender, int percentage);
public event ProgressEventDelegate Progress;
public void LongRunningTask()
{
for (int i = 0; i < 100; i++)
{
Thread.Sleep(1000);
this.Progress(this, i);
}
}
#endregion
#region " Backgroundworker "
public void LongRunningTask(BackgroundWorker bw)
{
for (int i = 0; i < 100; i++)
{
Thread.Sleep(1000);
bw.ReportProgress(i);
}
}
#endregion
}
}
mfg
Antworten
Vorsicht bei der Eingabe: Die Zeichen ' oder -- sind nicht erlaubt!