我正在开发一个使用WindowsFormsApplicationBase强制实施单一实例的应用程序。在Remote对象上调用方法时出现以下错误。如果我不使用单实例方法,它可以正常工作。
System.Runtime.Remoting.RemotingException:身份验证失败---> System.IO.IOException:无法从传输连接读取数据:连接已关闭。 在System.Net.Security.NegoState.ProcessAuthentication(LazyAsyncResult lazyResult) 在System.Net.Security.NegotiateStream.AuthenticateAsClient(NetworkCredential credential,String targetName,ProtectionLevel requiredProtectionLevel,TokenImpersonationLevel allowedImpersonationLevel) 在System.Runtime.Remoting.Channels.Tcp.TcpClientTransportSink.CreateAuthenticatedStream(Stream netStream,String machinePortAndSid)
这是我的代码:
public class EntryPoint
{
[STAThread]
public static void Main(string[] args)
{
SingleInstanceManager sim = new SingleInstanceManager();
sim.Run(args);
}
}
public class SingleInstanceManager : WindowsFormsApplicationBase
{
private App app;
public SingleInstanceManager()
{
IsSingleInstance = true;
}
protected override bool OnStartup(Microsoft.VisualBasic.ApplicationServices.StartupEventArgs eventArgs)
{
app = new App();
app.InitializeComponent();
app.Run();
return false;
}
protected override void OnStartupNextInstance(StartupNextInstanceEventArgs eventArgs)
{
base.OnStartupNextInstance(eventArgs);
app.Activate();
}
}
这就是我调用Remoting对象的方式:
public Hashtable GetData(string[] arg1, string[] arg2)
{
IDataProvider dataProvider = (IDataProvider )Activator.GetObject(typeof(IDataProvider ), "tcp://.....");
Hashtable data = dataProvider.GetData(arg1, arg2);
return data;
}
提前致谢。
答案 0 :(得分:0)
我自己找到了解决方案。
我使用以下方法实现单个实例( http://www.ai.uga.edu/mc/SingleInstance.html)。
[STAThread]
static void Main() // args are OK here, of course
{
bool ok;
m = new System.Threading.Mutex(true, "YourNameHere", out ok);
if (! ok)
{
MessageBox.Show("Another instance is already running.");
return;
}
Application.Run(new Form1()); // or whatever was there
GC.KeepAlive(m); // important!
}