我想构建一个c#应用程序——它应该像skype一样,在网页中创建链接,并向应用程序执行"callto"操作,传递参数。我希望应用程序只出现在任务栏中。
所以我第一次启动应用程序,然后下次单击链接时,它会在第一个应用程序上再次执行。
<a href="myapp:12345">Click me</a>
我将设置windows注册表以做出相应响应然后它应该用参数启动应用程序
例如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);
}