当前位置: 代码迷 >> C# >> 线程中动态回调宿主方法遇到点有关问题,怎么改
  详细解决方案

线程中动态回调宿主方法遇到点有关问题,怎么改

热度:107   发布时间:2016-05-05 02:57:51.0
线程中动态回调宿主方法遇到点问题,如何改?
线程代码:

delegate void CallThreadInvoke(int index);

public  virtual  void ThreadInvoke(int index); //这个地方不知如何处理?A窗体、B窗体都有这个方法,方法均不同。

 public static void StartThread(IntPtr handle)
        {   
            #region 线程回传数据
            Cls.ClsStaticVar.threadinvoke = new Thread(threadObj =>
            {               
                        Form.FromHandle(handle).Invoke(new CallThreadInvoke(ThreadInvoke), i);                                 
            });

            Cls.ClsStaticVar.threadinvoke.Start(); //线程开始
            #endregion
        }

问题是这样的,因为我的图形需要异步传输数据,上面代码简化了,使用了from.Invoke的方式回传数据。这里StartThread方法在A、B、C、X窗体均有出现,唯一不同的地方就是ThreadInvoke方法(名字都一样),于是我想重构StartThread方法,然后我将窗体的handle句柄传进来,然后就是这个ThreadInvoke不知道如何处理,如果把ThreadInvoke写成虚函数好像不行,写成实的delegate 那里又有问题。不知道如何处理是好。

描述不知道是否清楚,麻烦了,谢谢各位。

------解决思路----------------------
我不知道有没有会错你的意思,你是说你有 A,B,C,D4个窗口都会有提供一个函数,用来回传数据,但是你想封装的简单点是么?
如果是的话参考:


public static void Main (string[] args)
{
FormA frmA = new FormA ();
FormB frmB = new FormB ();
FormC frmC = new FormC ();
FormD frmD = new FormD ();
frmA.StartThread (1);
frmB.StartThread (2);
frmC.StartThread (3);
frmD.StartThread (4);
}

public class FormBase:Form
{
protected virtual void ThreadInvoke(int index)
{
Console.WriteLine ("I'm FormBase,the index:"+index+".");
}
public void StartThread(int index)
{

this.BeginInvoke (new Action<int>(ThreadInvoke), index);
}
}
public class FormA:FormBase
{
protected override void ThreadInvoke (int index)
{
Console.WriteLine ("I'm FormA,the index:"+index+".");
base.ThreadInvoke (index);
}
}
public class FormB:FormBase
{
protected override void ThreadInvoke (int index)
{
Console.WriteLine ("I'm FormB,the index:"+index+".");
base.ThreadInvoke (index);
}
}
public class FormC:FormBase
{
protected override void ThreadInvoke (int index)
{
Console.WriteLine ("I'm FormC,the index:"+index+".");
base.ThreadInvoke (index);
}
}
public class FormD:FormBase
{
protected override void ThreadInvoke (int index)
{
Console.WriteLine ("I'm FormD,the index:"+index+".");
base.ThreadInvoke (index);
}
}
  相关解决方案