在C#中单击按钮时,如何在单独的线程的标签页中打开新表单? [英] How do I open a new form in a tabpage on a separate thread when a button is clicked in C# ?

查看:123
本文介绍了在C#中单击按钮时,如何在单独的线程的标签页中打开新表单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

如何在现有tabControl的新标签页中打开新表单 / b>在单独的帖子上。我的线程概念非常尖锐,我不知道如何实现这一点。如果你可以帮我一些有用的网页和教程,或者甚至可以使用代码片段,那将是非常好的。



提前致谢! !!

解决方案

根据你的评论你想要的东西有点像MSWord ...



选项1 - 您可以启动程序的另一个实例 - 这将在它自己的线程中运行(我相信)。例如

 System.Diagnostics.Process proc = new System.Diagnostics.Process(); 
proc.StartInfo.FileName = Application.ExecutablePath;
proc.Start();



选项2 - 您可以在新线程下加载表格,例如

  var  t =  new  System.Threading.Thread(RunFormInNewThread); 
t.Start();
...
public static void RunFormInNewThread()
{
Application.Run( new Form1());
}

(这可能更接近于MSWord的工作方式,因为只有一个exe实例)



选项3 - 你可以在后台运行一个进程自己的线程(但我认为这不是你真正想要的)



这个链接可能有帮助线程教程 [ ^ ]

以及回复此SO帖子 [ ^ ]



还可以找到这个免费的电子书 http://www.albahari.com/线程/ [ ^ ]


使用多个UI线程是一个坏主意,它会给你很多奇怪的情况来维护但它可能是原因,只有相同的tabcontrol不能被多个线程拥有而且tabstrib是tabcontrol上的tabstrib集合中的一个控件。



所以标准获取具有自己的消息循环的表单的新线程我想你知道:

http://msdn.microsoft.com/en-us/library/ms157902.aspx



你想要做的是分开负载从显示中,你不是在另一个线程上打开它,而是在另一个线程上打开它。例如,考虑让TASK在为新标签加载数据时告诉您的用户界面,并且可以根据需要显示/选择/更新。

http://msdn.microsoft.com/en-us/library/jj155756.aspx



或者你可以采用更老式的方式并自己使用线程池:

http://msdn.microsoft.com/en-us/library/3dasc8as.aspx



最后一个选项是旧的偏爱被称为后台工作程序的包装器,您可以为每个tabstrib分配一个特定的,但我可以选择前者之一:http://msdn.microsoft.com/en-us/library/vstudio/system.componentmodel.backgroundworker



无论您使用何种方式,您都可以为任何a分配装载任务和任何其他任务将程序分配给工作线程并让gui闲置以取悦用户,直到结果准备好。

On一般自己处理线程,你可能会发现这个也很有用

http://msdn.microsoft.com/en-us/library /aa645740(v=vs.71).aspx

Hi All,
How may I open a "new form" in a new tabPage of an already existing tabControl on a separate thread. My concept of threading is very acute and i dont know how to accomplish this. If you may please help me out with some useful webpages and tutorials or may be even with the code snippet, it would be very kind of you.

Thanks in advance!!!

解决方案

Based on your comment that you want something that behaves a little like MSWord...

Option 1 - You could just start another instance of your program - this will be running in it's own thread (I believe). e.g.

System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo.FileName = Application.ExecutablePath;
proc.Start();


Option 2 - You can load the form (again) under a new thread e.g.

var t = new System.Threading.Thread(RunFormInNewThread);
    t.Start();
...
public static void RunFormInNewThread()
{
     Application.Run(new Form1());
}

(This is probably closer to how MSWord works as there is only one instance of the exe)

Option 3 - You can have a process running in the background on it's own thread (but I don't think this is really what you are after)

This link might help Threading Tutorial[^]
and also the responses to this SO post[^]

Also found this free e-book http://www.albahari.com/threading/[^]


It is a bad idea to use several UI threads, it will give you loads of strange situations to maintain but it is possible of cause, only the same tabcontrol cannot be owned by multiple threads and a tabstrib is a control in a collection of tabstribs on a tabcontrol.

so the standard getting a new thread for a form with own message loop i suppose you know:
http://msdn.microsoft.com/en-us/library/ms157902.aspx

What you want to do is to seperate the load from the displaying so that you are not opening it on another thread, but WITH another. consider for instance having a TASK tell your user interface when it has loaded data for your new tab and it can be shown/ selected/ updated as you wish.
http://msdn.microsoft.com/en-us/library/jj155756.aspx

or you could go for the more old fashioned way and use the threadpool yourself:
http://msdn.microsoft.com/en-us/library/3dasc8as.aspx

Last option is an old favoured wrapper known as the background worker, you could assign one specific for each tabstrib, but i'd propably go for one of the former: http://msdn.microsoft.com/en-us/library/vstudio/system.componentmodel.backgroundworker

Whatever way you use you can then assign the load task and any other for any assignment of the program to a work thread and let the gui idle around pleasing the user until the results are ready.
On Generally handling threads yourself, you may find this usefull as well
http://msdn.microsoft.com/en-us/library/aa645740(v=vs.71).aspx


这篇关于在C#中单击按钮时,如何在单独的线程的标签页中打开新表单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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