切换我的应用程序的线程。 [英] Switch between threads of my application.

查看:81
本文介绍了切换我的应用程序的线程。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个C#.NET应用程序。有一个主菜单。每当用户从主菜单中选择菜单项时,我都会使用Thread.Start()为他们选择创建一个新线程。这样,主菜单始终存在,以及当前打开的每个菜单项。用户可以使用Alt-Tab在主菜单和所有不同选项卡之间切换。



我要做的是从主菜单切换到应用程序控制下当前正在运行(并等待用户输入)的线程。我可以列出我的C#程序中的激活线程,并查看Thread.Name和Thread.ThreadState,它是正在运行。



所以我只想要一个C# .NET命令切换到其中一个正在运行的线程。我尝试了Thread.Interrupt(),但直到目标线程完成后才生效。我想要像Process.WaitForIdleInput()这样的东西,但是对于一个线程,而不是一个进程。我的整个应用程序,包括所有线程,只显示为一个进程。我需要一个与Alt-Tab完全相同的命令,切换到正确的线程。



这看起来很简单,但我一直无法理解。感谢您的帮助。



我很感谢到目前为止的评论,但我似乎没有说清楚我在寻找什么。目前,我的应用程序可以有多个线程。每个对应于菜单中的任务,它们各自收集输入和处理事务。因此,在给定时间,某些人可能正在等待输入,而其他人正在处理。我不打算重写这个,无论它在这里产生多大的愤怒。



用户可以使用Alt-Tab在线程之间切换。因此,他们可能会启动运行报告,然后将Alt-Tab启动到另一个线程,并启动其他报告或输入事务。然后他们可以将Alt-Tab返回,并查看他们的报告。我想要的只是让他们能够点击我提供的按钮,以便切换到不同的线程,就好像他们已经键入Alt-Tab一样。单击该按钮时,我可以捕获它,并且我可以生成正在运行的程序线程列表。我想在那时执行一个命令,它将切换到其中一个线程。如果我能做到这一点,那很好。如果没有,我仍然没有重写我的应用程序,用户可以继续使用Alt-Tab。我希望这会让事情变得更清楚,我很欣赏这些建议。



我尝试过的事情:



我已经尝试了所有似乎可以工作的线程方法:Join(),Interrupt()等。



我试过Process命令,但我的整个应用程序只显示为一个进程。

解决方案

实际上,从基本的角度来看,你的想法更有意义解决方案1状态。你所描述的内容大致与合作非预先多任务处理的概念相符:

合作多任务 - 维基百科,免费的百科全书 [ ^ ]。



另请参阅我对解决方案1的评论。



此外,从OS实现的角度来看,协作式多任务可以被视为任何其他多任务和线程切换的基础。假设在某些实现中,您可能会创建一个抢占式多任务处理机制,而不是添加硬件中断处理,它将控制转移到使用协作交换机调用来激活一个或另一个线程的调度程序。实际上,这更复杂,因为CICS CPU(复杂的指令集计算 - 维基百科,免费的百科全书 [ ^ ]),与Intel x86 *和IE- *架构一样,都有线程切换和中断支持已经在他们的微代码中投射。



但是,您应该意识到您正在使用完全基于纯的线程模型的操作系统先发制人多任务范例:

抢占(计算) - 维基百科,免费的百科全书 [ ^ ]。



这并不意味着您根本无法访问任务切换。在Windows API中,有一个非常奇特的部分允许用户实现协作切换的线程。只有它们不被称为线程,被称为纤维

光纤(计算机科学) - 维基百科,免费的百科全书 [ ^ ],

https:// msdn .microsoft.com / zh-CN / library / ms682661.aspx [ ^ ]。



走这条路是否有意义? 我不这么认为;不是在你的情况下

我觉得你对线程切换的想法并不是基于一些非常特殊的线程方法的深刻和富有成效的想法,而是缺乏对使用的操作系统如何与应用程序配合以及如何真正设计应用程序的认识。



从技术上讲,基于光纤的开发需要你使用P / Invoke,这是一种完全杀死你的平台兼容性的可靠方法,如果你还有它。



-SA

否。你完全有错误的想法。

UI线程应该负责所有用户输入 - 没有Invoke,线程就无法访问UI组件 - 这会将执行移回UI线程。



我认为你要做的是通过启动一个启动新应用程序的线程来显示不同的应用程序或应用程序的不同视图 - 这是错误的继续方式。相反,要么使用Process.Start来执行应用程序,然后使用Process对象通过Process.MainWindowHandle和非托管的Win32 API函数ShowWindow来激活窗口。



如果您试图将所有这些保留在一个应用程序中,那么只需为每个菜单选项创建并显示一个新表单,您可以使用Activate方法将其直接带到前面。



但我认为你不需要在这里直接使用线程。


I have a C#.NET application. There is a main menu. Whenever the user chooses a menu item from the main menu, I create a new thread for their choice, with Thread.Start(). That way, the main menu is always present, along with each menu item that is presently open. The user can use Alt-Tab to switch between the main menu and all the different tabs.

What I am trying to do is switch from the main menu, to a thread that is currently running (and waiting for user input), under application control. I can list the activate threads in my C# program, and see the Thread.Name and Thread.ThreadState, which is "Running".

So I just want a C#.NET command to switch to one of the running threads. I tried Thread.Interrupt(), but that does not take effect until the target thread has completed. I want something like Process.WaitForIdleInput(), but for a thread, not a process. My whole application, including all the threads, appears as only a single process. I need a command which does the same thing as Alt-Tab, switching to the correct thread.

This seems simple, but I have been unable to figure it out. Thanks for any assistance.

I appreciate the comments so far, but I do not seem to have made clear what I am looking for. At present, there can be multiple threads of my application. Each one corresponds to a task in a menu, they each gather input, and process transactions. So at a given time some might be waiting for input, while others were processing. I am not planning to rewrite this, no matter how much outrage it creates here.

The user is able to use Alt-Tab to switch between the threads. So they might start a report running, then Alt-Tab to another thread, and initiate a different report, or input a transaction. Then they could Alt-Tab back, and view their report. All I want is for them to be able to click a button I supply them, in order to switch to a different thread, just as if they had keyed Alt-Tab. When the button is clicked, I can capture it, and I can generate a list of the program threads that are running. I would like to at that point execute a command, that would switch to one of those threads. If I can do that, great. If not, I am still not rewriting my application, the users can keep using Alt-Tab instead. I hope this has made things clearer, I appreciate the suggestions.

What I have tried:

I have tried all the thread methods that seemed like they might work: Join(), Interrupt(), etc.

I tried Process commands, but my whole application only appears as a single process.

解决方案

Actually, your idea, from the fundamental point of view, makes much more sense that the Solution 1 states. What you describe roughly matches the concept of cooperative, non-preemtive multitasking:
Cooperative multitasking — Wikipedia, the free encyclopedia[^].

See also my comment to Solution 1.

Moreover, from the OS implementation standpoint, cooperative multitasking can be considered as a basis of any other multitasking and thread switching. Let's say, in some implementations, it could be possible that you create a mechanism for preemptive multitasking, and than add hardware interrupt handling which transfers control to a scheduler which uses cooperative switch call to activate one or another thread. In reality, this is more complicated, because CICS CPUs (Complex instruction set computing - Wikipedia, the free encyclopedia[^]), like that of Intel x86* and IE-* architectures, have both thread switching and interrupt support already cast in their microcode.

However, you should realize that you are working with the OS with the threading model based exclusively on pure preemptive multitasking paradigm:
Preemption (computing) — Wikipedia, the free encyclopedia[^].

It does not mean that you don't have access to task switching at all. In Windows API, there is one quite exotic part which allows the user to implement cooperatively switched threads. Only they are not called "threads", the are called fibers:
Fiber (computer science) — Wikipedia, the free encyclopedia[^],
https://msdn.microsoft.com/en-us/library/ms682661.aspx[^].

Does it make any sense to go this way? I don't think so; not in your case.
It seems apparent to me that your ideas on thread switching are not based on some deep and fruitful idea on very special way of threading for some very special class of application, but rather on lack of awareness on how the OS you are using works with applications and how the applications should really be designed.

Technically, development based on fibers will require you to use P/Invoke, which is a sure way to totally kill your platform compatibility, if you still have it.

—SA


No. You have the wrong idea completely.
The UI thread should be responsible for all user input - threads don't have access to UI components without Invoke - which moves execution back to the UI thread.

I think what you are trying to do is display different applications, or different views on an application by kicking off a thread which starts a new application - that's the wrong way to proceed. Instead, either use Process.Start to execute the application, and then use the Process object to activate the window by via the Process.MainWindowHandle and the unmanaged Win32 API function ShowWindow.

If you are trying to keep all these within the one application, then then just create and Show a new form for each menu selection and you can bring it to the front directly with the Activate method.

But I don't think you need to be using threads directly here.


这篇关于切换我的应用程序的线程。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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