C# 实例感知应用程序传递参数窗口窗体应用程序
我想构建一个c#应用程序——它应该像skype一样,在网页中创建链接,并向应用程序传递参数,执行“callto”操作。我希望应用程序仅位于任务栏中 因此,我第一次启动应用程序,然后下次单击链接时,它将在第一个应用程序上再次执行C# 实例感知应用程序传递参数窗口窗体应用程序,c#,winforms,C#,Winforms,我想构建一个c#应用程序——它应该像skype一样,在网页中创建链接,并向应用程序传递参数,执行“callto”操作。我希望应用程序仅位于任务栏中 因此,我第一次启动应用程序,然后下次单击链接时,它将在第一个应用程序上再次执行 <a href="myapp:12345">Click me</a> 我将设置windows注册表以作出相应的响应 然后它应该使用参数启动应用程序 e、 g.myapp.exe/12345 我可以让应用程序工作,但每次单击链接都会得到一个新实
<a href="myapp:12345">Click me</a>
我将设置windows注册表以作出相应的响应
然后它应该使用参数启动应用程序
e、 g.myapp.exe/12345
我可以让应用程序工作,但每次单击链接都会得到一个新实例,如何将参数传递给当前正在运行的应用程序?灵感来源于防止出现第二个实例和namedpipeserver
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 hasHandle = false;
try
{
try
{
hasHandle = mutex.WaitOne(5000, false);
}
catch (AbandonedMutexException)
{
hasHandle = true;
}
if (hasHandle)
{
// main app
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Form1 frm = new Form1();
HandleArgs(args,frm); // handle first start with args
Server(appGuid, frm); // handle next clients
Application.Run(frm);
}
else
{
//any next client..
var cli = new NamedPipeClientStream(appGuid);
// connect to first app
cli.Connect();
// serialiaze args and send over
var bf = new BinaryFormatter();
bf.Serialize(cli, args); // the commandline args over the line
cli.Flush();
cli.Close();
// done
}
}
finally
{
if (hasHandle)
mutex.ReleaseMutex();
}
}
}
// do usefull stuff with the commandline args
static void HandleArgs(string[] args, Form1 frm)
{
foreach (var s in args)
{
// just append the args to the textbox
frm.textBox1.Text += s;
frm.textBox1.Text += Environment.NewLine;
}
}
// server that runs async
static void Server(string appGuid, Form1 frm)
{
var srv = new NamedPipeServerStream(appGuid, PipeDirection.InOut, 5, PipeTransmissionMode.Byte, PipeOptions.Asynchronous);
srv.BeginWaitForConnection(state =>
{
NamedPipeServerStream nps = (NamedPipeServerStream)state.AsyncState;
nps.EndWaitForConnection(state);
var bf = new BinaryFormatter();
string[] args = (string[])bf.Deserialize(nps);
// don't forget to call on the UI thread
frm.Invoke(new MethodInvoker(() => { HandleArgs(args, frm); }));
nps.Disconnect();
Server(appGuid, frm); // restart server
}, srv);
}
灵感来源于防止第二个实例和namedpipeserver
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 hasHandle = false;
try
{
try
{
hasHandle = mutex.WaitOne(5000, false);
}
catch (AbandonedMutexException)
{
hasHandle = true;
}
if (hasHandle)
{
// main app
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Form1 frm = new Form1();
HandleArgs(args,frm); // handle first start with args
Server(appGuid, frm); // handle next clients
Application.Run(frm);
}
else
{
//any next client..
var cli = new NamedPipeClientStream(appGuid);
// connect to first app
cli.Connect();
// serialiaze args and send over
var bf = new BinaryFormatter();
bf.Serialize(cli, args); // the commandline args over the line
cli.Flush();
cli.Close();
// done
}
}
finally
{
if (hasHandle)
mutex.ReleaseMutex();
}
}
}
// do usefull stuff with the commandline args
static void HandleArgs(string[] args, Form1 frm)
{
foreach (var s in args)
{
// just append the args to the textbox
frm.textBox1.Text += s;
frm.textBox1.Text += Environment.NewLine;
}
}
// server that runs async
static void Server(string appGuid, Form1 frm)
{
var srv = new NamedPipeServerStream(appGuid, PipeDirection.InOut, 5, PipeTransmissionMode.Byte, PipeOptions.Asynchronous);
srv.BeginWaitForConnection(state =>
{
NamedPipeServerStream nps = (NamedPipeServerStream)state.AsyncState;
nps.EndWaitForConnection(state);
var bf = new BinaryFormatter();
string[] args = (string[])bf.Deserialize(nps);
// don't forget to call on the UI thread
frm.Invoke(new MethodInvoker(() => { HandleArgs(args, frm); }));
nps.Disconnect();
Server(appGuid, frm); // restart server
}, srv);
}
看一看如何防止第二个实例,对于参数的传递,您必须在第一个应用程序中创建一个侦听器,在第二个应用程序中创建一个发送器。命名管道可以执行此操作,或者一个管道读取另一个管道写入的文件。如何将参数传递到当前运行的应用程序?好的,谢谢,我应该使用哪种类型的侦听器?args=Environment.GetCommandLineArgs();我使用的是static void Main(string[]MyArgs),TCP服务器是最简单的,因为它附带了aync行为。为了传递参数,您必须在第一个应用程序中创建一个侦听器,在第二个应用程序中创建一个发送器。命名管道可以执行此操作,或者一个管道读取另一个管道写入的文件。如何将参数传递到当前运行的应用程序?好的,谢谢,我应该使用哪种类型的侦听器?args=Environment.GetCommandLineArgs();我使用的是静态voidmain(string[]MyArgs),TCP服务器是最简单的,因为它具有aync行为