问题描述
我想创建一个表单并将一个列表框放入其中,然后用另一个线程中的一些信息通过ma更新它,使用多线程和代理和事件使用
也请明确表示有关使用后台工作人员执行此任务的信息
i want to create a form and put a list box into it and then update it with some information from another thread by ma,king use of multi threading and delegates and events
also please give explicit information about doing this task using background worker
推荐答案
private void button1_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2(this);//this is required to update list by calling public method in Form1. Not required for event based update
form2.OnMyForm2Event += AddToListViewByListeningToEvent;
form2.Show();
}
public void AddToListUsingPublicProperty(string item)
{
listBox1.Invoke(new MethodInvoker(() =>
{
listBox1.Items.Add(item);
}));
}
private void AddToListViewByListeningToEvent(object sender, MyEventArgs e)
{
listBox1.Invoke( new MethodInvoker( () =>
{
listBox1.Items.Add(e.EventData);
}));
}
------------------------------ ------------------------------------------
FORM2代码
---------------------------------------- ---------------------------------------
公共部分班级表格2:表格
{
公共活动EventHandler< myeventargs> OnMyForm2Event;
BackgroundWorker _bw = new BackgroundWorker();
public Form1 _Form1;
public Form2()
{
InitializeComponent();
}
public Form2(Form1) form1)
{
InitializeComponent();
this._Form1 = form1;
_bw.DoWork + = DoBackgroundWork;
_bw.RunWorkerCompleted + = OnRunWorkerCompleted;
}
#region BackgroundWorker
private void btnUsingBackgroundWorker_Click(object sender,EventArgs e)
{
_bw.RunWorkerAsync();
}
private void DoBackgroundWork(对象发送者,DoWorkEventArgs e)
{
//做一些重型操作口粮
if(OnMyForm2Event!= null)
OnMyForm2Event(这个,新的MyEventArgs(ItemFromForm2UsingBGThread));
//通过调用Form1上的公共属性添加项目
_Form1.AddToListUsingPublicProperty(ItemAddedDirectlyByCallingPublicMethod-BackGroundWorker);
}
private void OnRunWorkerCompleted(对象发送者,RunWorkerCompletedEventArgs e)
{
_Form1.AddToListUsingPublicProperty(ItemAddedWhenBackgroundworkerFinisheswork);
}
#endregion
#region线程
private void btnUpdateUsingThread_Click(object sender,EventArgs e)
{
线程t =新线程(PerformActionOnThread);
t.Start(ItemFromForm2UsingThread);
}
private void PerformActionOnThread(object value)
{
//使用事件添加
if(OnMyForm2Event!= null)
OnMyForm2Event(这个,新的MyEventArgs(value.ToString()));
//通过调用Form1上的公共属性添加项目
_Form1.AddToListUsingPublicProperty(ItemAddedDirectlyByCallingPublicMethod-Thread);
}
#endregion
#region任务
private void btnUsingTask_Click(object sender,EventArgs e)
{
Task.Factory.StartNew(( )=> _Form1.AddToListUsingPublicProperty(ItemAddedByTask))。
ContinueWith(x => _Form1.AddToListUsingPublicProperty(ItemAddedOnTaskCompletion));
}
#endregion
}
公共类MyEventArgs:EventArgs
{
public string EventData {get;组; } $ / $
public MyEventArgs(string eventData)
{
EventData = eventData;
}
}
------------------------------------------------------------------------
FORM2 CODE
-------------------------------------------------------------------------------
public partial class Form2 : Form
{
public event EventHandler<myeventargs> OnMyForm2Event;
BackgroundWorker _bw = new BackgroundWorker();
public Form1 _Form1;
public Form2()
{
InitializeComponent();
}
public Form2(Form1 form1)
{
InitializeComponent();
this._Form1 = form1;
_bw.DoWork += DoBackgroundWork;
_bw.RunWorkerCompleted += OnRunWorkerCompleted;
}
#region BackgroundWorker
private void btnUsingBackgroundWorker_Click(object sender, EventArgs e)
{
_bw.RunWorkerAsync();
}
private void DoBackgroundWork(object sender, DoWorkEventArgs e)
{
//Do some heavy duty operations
if (OnMyForm2Event != null)
OnMyForm2Event(this, new MyEventArgs("ItemFromForm2UsingBGThread"));
//Add item by calling public property on Form1
_Form1.AddToListUsingPublicProperty("ItemAddedDirectlyByCallingPublicMethod-BackGroundWorker");
}
private void OnRunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
_Form1.AddToListUsingPublicProperty("ItemAddedWhenBackgroundworkerFinisheswork");
}
#endregion
#region Thread
private void btnUpdateUsingThread_Click(object sender, EventArgs e)
{
Thread t = new Thread(PerformActionOnThread);
t.Start("ItemFromForm2UsingThread");
}
private void PerformActionOnThread(object value)
{
//Add using Event
if (OnMyForm2Event != null)
OnMyForm2Event(this, new MyEventArgs(value.ToString()));
//Add item by calling public property on Form1
_Form1.AddToListUsingPublicProperty("ItemAddedDirectlyByCallingPublicMethod-Thread");
}
#endregion
#region Task
private void btnUsingTask_Click(object sender, EventArgs e)
{
Task.Factory.StartNew(() => _Form1.AddToListUsingPublicProperty("ItemAddedByTask")).
ContinueWith(x => _Form1.AddToListUsingPublicProperty("ItemAddedOnTaskCompletion"));
}
#endregion
}
public class MyEventArgs : EventArgs
{
public string EventData { get; set; }
public MyEventArgs(string eventData)
{
EventData = eventData;
}
}
这篇关于如何创建具有列表框的表单,并使用多线程和委托从另一个表单更新它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!