如何在c#中共享在同一个exe的多个实例上运行的线程 [英] how to share threads running on multiple instance of same exe in c#
问题描述
您好,这是yogesh夏尔马,我使用c#开发了客户端服务器Windows应用程序。现在我已经在mdi表单的load事件上启动了一个名为thread1的线程(在我退出程序时结束)。
我的问题是,当我在同一台或另一台PC上启动相同应用程序的另一个实例时,我需要获取thread1的状态。
我的代码是在我的mdi表单上,我的代码是:
Hi This is yogesh sharma and i have developed client server windows application using c#. Now i have started a thread named thread1(which ends when i quit the programme) on the load event of the mdi form.
My problem is that i need to get the state of thread1 when i have started another instance of same application on same or another PC.
My code is below.
On my mdi form my code is:
Global.background = new Thread(new ThreadStart(() =>
{
while (true)
{
Thread.Sleep(50);
}
}
));
这里Global是一个我定义线程的类
here Global is a class where i defined the thread as
public static Thread background;
i需要知道背景的状态从应用程序的另一个实例线程thread1(运行,unstarted,停止等)。请帮助我如何从我的应用程序的每个实例共享global.backgound ..任何帮助将不胜感激...
i need to know the state of the background thread thread1(running,unstarted,stopped etc) from another instance of the application..Please help how could i share global.backgound from each instance of my application..Any Help will be appreciated...
推荐答案
不可能:线程存在于进程内部 - 一旦进程终止,线程就会死掉。
您可能会设计面向服务的体系结构(SOA):
- 一个服务保存状态(你的线程)
- GUI实例与服务通信
干杯
Andi
Not possible: threads live inside of processes - once a process dies, the threads die too.
You might design a service oriented architecture (SOA):
- one service holds the state (your thread)
- the GUI instances communicate with the service
Cheers
Andi
Hello Yogesh,
你无法共享你的例子中的主题 EXE的另一个实例,你创建一个不同的进程来运行它。
每个进程使用它自己的内存空间。
您可能会重新考虑创建第二个流程,或者您可以使用进程间通信的各种方法之一( IPC ),例如:
- 命名管道
- 共享内存
- MSMQ
- 套接字
干杯,
Edo
Hello Yogesh,
You can not share the threads in your example since when you start another instance of the EXE, you create a different process for it to run in.
Each process uses it''s own memory "space".
You may re-think creating a second process OR you can one of the various methods for Inter-Process Communication (IPC), for example:
- Named Pipes
- Shared Mem
- MSMQ
- Sockets
Cheers,
Edo
这篇关于如何在c#中共享在同一个exe的多个实例上运行的线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!