一个简单的回调函数实现
public void ReportForm()
{
MM m = new MM();
m.mm(xx); //传入需要回调函数
}
public void xx(string aa)
{
Console.WriteLine(aa);
}
public class MM
{
//
Action一个有参数无返回值的委托方法,一般回调函数需要一个参数
public void mm(Action<string> predicate)
{
predicate("hello!!");//调用传的方法形成回调
}
}
一个带参数跨线程函数实现
线程类(如果线程执行的方法需要传参数可以把他单独写到一个类里)
public class ThreadDepart
{
SynchronizationContext syn = new SynchronizationContext();
RealTask.RealTaskClient rc;
string sessinid;
ObservableCollection<RealTask.VehicleStartTime> rsesult;
int lineid;
DateTime beginTime;
Action<ObservableCollection<RealTask.VehicleStartTime>> f;//需要传入一个带参数的委托方法
public ThreadDepart(ObservableCollection<RealTask.VehicleStartTime> rsesult, int lineid, DateTime beginTime, RealTask.RealTaskClient rc, string sessinid, Action<ObservableCollection<RealTask.VehicleStartTime>> f)
{
this.rsesult = rsesult;
this.lineid = lineid;
this.beginTime = beginTime;
this.rc = rc;
this.sessinid = sessinid;
this.f = f;
rc.RealStartRunningTimeCompleted += new EventHandler<RealTask.RealStartRunningTimeCompletedEventArgs>(rc_RealStartRunningTimeCompleted);
}
void rc_RealStartRunningTimeCompleted(object sender, RealTask.RealStartRunningTimeCompletedEventArgs e)
{
rsesult = e.Result.Value;
if (rsesult != null && rsesult.Count > 0)
{
beginTime = rsesult[rsesult.Count - 1].RealStartRunningTimek__BackingField;
//这里接收到值需要提醒前台修改界面
//f(rsesult);//如果直接执行前台的方法会报跨线程访问无效
Deployment.Current.Dispatcher.BeginInvoke(f, rsesult);//用这里方法调用就行了 rsesult是回调函数需要的参数
}
}
public void ThreadRealTime()//线程需要执行的方法
{
while (true)//需要一直监听后台的数据
{
rc.RealStartRunningTimeAsync(sessinid,lineid,beginTime);
System.Threading.Thread.Sleep(120000);
}
}
}
{
SynchronizationContext syn = new SynchronizationContext();
RealTask.RealTaskClient rc;
string sessinid;
ObservableCollection<RealTask.VehicleStartTime> rsesult;
int lineid;
DateTime beginTime;
Action<ObservableCollection<RealTask.VehicleStartTime>> f;//需要传入一个带参数的委托方法
public ThreadDepart(ObservableCollection<RealTask.VehicleStartTime> rsesult, int lineid, DateTime beginTime, RealTask.RealTaskClient rc, string sessinid, Action<ObservableCollection<RealTask.VehicleStartTime>> f)
{
this.rsesult = rsesult;
this.lineid = lineid;
this.beginTime = beginTime;
this.rc = rc;
this.sessinid = sessinid;
this.f = f;
rc.RealStartRunningTimeCompleted += new EventHandler<RealTask.RealStartRunningTimeCompletedEventArgs>(rc_RealStartRunningTimeCompleted);
}
void rc_RealStartRunningTimeCompleted(object sender, RealTask.RealStartRunningTimeCompletedEventArgs e)
{
rsesult = e.Result.Value;
if (rsesult != null && rsesult.Count > 0)
{
beginTime = rsesult[rsesult.Count - 1].RealStartRunningTimek__BackingField;
//这里接收到值需要提醒前台修改界面
//f(rsesult);//如果直接执行前台的方法会报跨线程访问无效
Deployment.Current.Dispatcher.BeginInvoke(f, rsesult);//用这里方法调用就行了 rsesult是回调函数需要的参数
}
}
public void ThreadRealTime()//线程需要执行的方法
{
while (true)//需要一直监听后台的数据
{
rc.RealStartRunningTimeAsync(sessinid,lineid,beginTime);
System.Threading.Thread.Sleep(120000);
}
}
}
前台
public void InitThreadDepart()//开启线程进行监听
{
System.Collections.ObjectModel.ObservableCollection<RealTask.VehicleStartTime> v = new System.Collections.ObjectModel.ObservableCollection<RealTask.VehicleStartTime>();
ThreadDepart td = new ThreadDepart(v, 86, DateTime.Parse("06:00"), rc, SessionID, aa);//实例化线程类通过构造方法赋值aa为需要的回调函数
ThreadStart ts = new ThreadStart(td.ThreadRealTime);
Thread th = new Thread(ts);
th.Start();
}
{
System.Collections.ObjectModel.ObservableCollection<RealTask.VehicleStartTime> v = new System.Collections.ObjectModel.ObservableCollection<RealTask.VehicleStartTime>();
ThreadDepart td = new ThreadDepart(v, 86, DateTime.Parse("06:00"), rc, SessionID, aa);//实例化线程类通过构造方法赋值aa为需要的回调函数
ThreadStart ts = new ThreadStart(td.ThreadRealTime);
Thread th = new Thread(ts);
th.Start();
}
//回调方法
public void aa(System.Collections.ObjectModel.ObservableCollection<RealTask.VehicleStartTime> vst)
{
//修改界面的代码, vst需要放到界面的数据
}
{
//修改界面的代码, vst需要放到界面的数据
}