C# 单个进程winform应用程序上的多个应用程序域
我正在使用program.cs文件中的代码在winform应用程序中创建应用程序域C# 单个进程winform应用程序上的多个应用程序域,c#,appdomain,C#,Appdomain,我正在使用program.cs文件中的代码在winform应用程序中创建应用程序域 static class Program { /// <summary> /// The main entry point for the application. /// </summary> /// [STAThread] static void Main(string[] args) { string appG
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
///
[STAThread]
static void Main(string[] args)
{
string appGuid = ((GuidAttribute)Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(GuidAttribute), false).GetValue(0)).Value.ToString();
// unique id for global mutex - Global prefix means it is global to the machine
string mutexId = string.Format("Global\\{{{0}}}", appGuid);
using (var mutex = new Mutex(false, mutexId))
{
var allowEveryoneRule = new MutexAccessRule(new SecurityIdentifier(WellKnownSidType.WorldSid, null), MutexRights.FullControl, AccessControlType.Allow);
var securitySettings = new MutexSecurity();
securitySettings.AddAccessRule(allowEveryoneRule);
mutex.SetAccessControl(securitySettings);
if (mutex.WaitOne(TimeSpan.Zero, true) || (args.Length > 0 && string.Compare(args[0], "secondary", true) == 0))
{
ErrorHandler errorHandler = new ErrorHandler();
DffEnvironment.Default.AppErrorHandler = errorHandler;
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(errorHandler.Application_ErrorHandler);
MainForm mainForm = new MainForm();
DffEnvironment.Default.MainForm = mainForm;
if (args.Length > 0)
{
MessageBox.Show(" CurrentDomain" + AppDomain.CurrentDomain.FriendlyName);
}
Application.Run(mainForm);
}
else
{
// send our Win32 message to make the currently running instance
// Add new app domain
NativeMethods.PostMessage(
(IntPtr)NativeMethods.HWND_BROADCAST,
NativeMethods.WM_SHOWME,
IntPtr.Zero,
IntPtr.Zero);
}
}
}
}
它工作正常,当我尝试打开我的应用程序时,它会在已经运行的同一进程中创建另一个应用程序域
我的第一个问题是,例如,我是否可以创建其他用户已经运行的同一进程的应用程序域
John正在开发此应用程序,拥有两个应用程序域和一个进程。Steve登录到同一台机器并试图打开此应用程序,应用程序不应创建进程,它应在John已经运行的进程中添加新的应用程序域
我通过在互斥体的名称前加上“Global\”检测到进程正在另一个用户中运行。如上所述
当我在program.cs中编辑以下代码时,第二个问题就在这里
NativeMethods.PostMessage(
(IntPtr)NativeMethods.HWND_BROADCAST,
NativeMethods.WM_SHOWME,
IntPtr.Zero,
IntPtr.Zero);
到
它创建了另一个进程,为什么它不能在Program.cs文件中工作,为什么我必须将消息发送到我的表单并在WndProc方法中执行相同的操作,我将回答您的第二个问题 应用程序域是进程内部的隔离环境。这里有两个进程,它们都有自己的应用程序域。如果要命令另一个进程创建新的应用程序域,则必须从一个进程向另一个进程发送消息。这就是为什么你必须发送信息 我还怀疑代码没有按照您预期的方式工作。ExecuteAssembly()与主用户界面在同一线程中运行。如果执行的程序集开始一个新的消息循环,那么在每个WM_SHOWME消息之后,您的调用堆栈将增长,最终您将得到堆栈溢出异常 在这种情况下,您的调用堆栈大致如下所示:
at Application.Run()
at Main()
at AppDomain.ExecuteAssembly()
...
at Application.Run()
at Main()
at AppDomain.ExecuteAssembly()
at Application.Run()
at Main()
我来回答你的第二个问题 应用程序域是进程内部的隔离环境。这里有两个进程,它们都有自己的应用程序域。如果要命令另一个进程创建新的应用程序域,则必须从一个进程向另一个进程发送消息。这就是为什么你必须发送信息 我还怀疑代码没有按照您预期的方式工作。ExecuteAssembly()与主用户界面在同一线程中运行。如果执行的程序集开始一个新的消息循环,那么在每个WM_SHOWME消息之后,您的调用堆栈将增长,最终您将得到堆栈溢出异常 在这种情况下,您的调用堆栈大致如下所示:
at Application.Run()
at Main()
at AppDomain.ExecuteAssembly()
...
at Application.Run()
at Main()
at AppDomain.ExecuteAssembly()
at Application.Run()
at Main()
为什么需要单例进程?你想达到什么目的?为什么需要单例过程?你们想实现什么?是的,我可以理解,但我可以在WndProc方法中检查它是否应该在这个场景中调用。我设法做到了。我想知道的是,有没有办法让app domain出现在program.cs文件中。是的,我可以理解,但我可以在WndProc方法中检查它是否应该在这种情况下调用。我设法做到了。我想知道的是,有没有办法让app-domain出现在program.cs文件中。
at Application.Run()
at Main()
at AppDomain.ExecuteAssembly()
...
at Application.Run()
at Main()
at AppDomain.ExecuteAssembly()
at Application.Run()
at Main()