c#中的覆盖和线程 [英] Overriding and Threading in c#

查看:77
本文介绍了c#中的覆盖和线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hi


我是一个派生自名为BaseForm1的表格的类。看起来好像



  public   BaseForm1:表格
{

public BaseForm1()
{
}
受保护 覆盖 void OnLoad(EventArgs e)
{
base .OnLoad(e);
线程t = new 线程( new ThreadStart(Init));
t.Start();
}

public void Init()
{
/ * 此函数可以对表单加载进行一些操作,例如清除文本框,
loading数据到组合框等。

调用此函数会使表单加载缓慢。所以我在新线程中调用它

* /

}

}





我有另一个类继承自上面的类



在这个课程中我改写了函数Init()









  public  覆盖  void  Init()
{
base .Init();

textbox1.Text = Some Text;
CallSomeFunction();
}







执行此代码后会发生以下错误



跨线程操作无效:控制'textbox1'从其创建的线程以外的线程访问。





我创建了以下函数来调用init

  private   void  CallInit()
{
this .Invoke( new MethodInvoker( delegate ()
{
Init();
}) );
}



并尝试启动以下线程

  protected  覆盖  void  OnLoad(EventArgs e)
{
base .OnLoad(e);
线程t = new 线程( new ThreadStart(CallInit));
t.Start();
}





并尝试过BackgroundWorker



DoWork( )也给出了同样的错误



RunWorkerCompleted()没有给出所需的性能



请帮忙! div class =h2_lin>解决方案

必须在UI线程上执行UI对象上的线程操作。你得到错误是因为你在基类中创建调用派生方法init例程的线程。



对于每个文本框,你应该做这样的事情:



 myTextBox.Invoke((MethodInvoker) delegate  {myTextBox。 Text =  string  .Empty;}); 





您可以查看是否调用是必需的,但是如果你在一个单独的线程上运行那么它将是(使用myTextBox.InvokeRequired看看你是否必须执行上述操作)。



它但问题是,你在一个表单上有这么多的文本框,你的初始化例程必须被线程化吗?为什么不将它们设置为设计器中的默认值?如果初始化表单需要很长时间,也许您想要将其分解为可以在需要时加载的逻辑部分。


Hi
I've a class derived from form called "BaseForm1". It looks like

public class BaseForm1 : Form
{

   public BaseForm1()
{
}
 protected override void OnLoad(EventArgs e)
{
  base.OnLoad(e);
  Thread t = new Thread(new ThreadStart(Init));
  t.Start();
}

public void Init()
{
/* This function do some work on form load such as clearing text boxes ,
loading data to combobox etc.

Calling this function makes the form load slowly. So I called it in new thread

*/
}

}



I have another class inherits from the above class

In this class I overrides the function "Init()"


like

public override void Init()
{
base.Init();

textbox1.Text = "Some Text";
 CallSomeFunction();
}




When this code executes following error occures

Cross-thread operation not valid: Control 'textbox1' accessed from a thread other than the thread it was created on.



I Created the following function to call the init

private void CallInit()
       {
           this.Invoke(new MethodInvoker(delegate()
           {
               Init();
           }));
       }


and tried to start the thread like below

protected override void OnLoad(EventArgs e)
     {
         base.OnLoad(e);
         Thread t = new Thread(new ThreadStart(CallInit));
         t.Start();
     }



and also tried BackgroundWorker

DoWork() also gives the same error

RunWorkerCompleted() not gives the required performance

please help !

解决方案

Thread operations on UI objects have to be performed on the UI thread. You get the error because you are creating the thread in the base class which calls the derived method init routine.

For each of the textboxes, you should do something like this:

myTextBox.Invoke((MethodInvoker) delegate { myTextBox.Text = string.Empty; });



You can check if invoke is required but if you are running on a separate thread then it will be (use the myTextBox.InvokeRequired to see if you have to do the above).

It begs the question though, do you have so many text boxes on one form that your initialization routine has to be threaded? Why not set them to the defaults in the designer? If it takes that long to initialize your form, maybe you want to think about breaking it up into logical pieces that can be loaded when needed.


这篇关于c#中的覆盖和线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆