当主线程去访问一个静态类的方法,而刚好这个方法是要从网络上取得返回值的,这时会报错。那这个静态类方法里面应该如何来写呢?万分感谢
比如我有类
public static NetHelper
{
public static getVersion()
{
int version = getNetVersion();//这里从网络上取得网上的版本
}
}
public static class NetHelper
{
internal sealed class proxy
{
private object waitLock = new object();
private int version;
private bool isValue;
private void getNetVersion()
{
version = NetHelper.getNetVersion();
Monitor.Enter(waitLock);
try
{
isValue = true;
Monitor.Pulse(waitLock);
}
finally { Monitor.Exit(waitLock); }
}
private int wait()
{
Monitor.Enter(waitLock);
try
{
if (!isValue) Monitor.Wait(waitLock);
}
finally { Monitor.Exit(waitLock); }
return version;
}
public static int GetNetVersion()
{
proxy proxy = new proxy();
new Thread(proxy.getNetVersion).Start();//当然最好是丢在线程池里面
return proxy.wait();
}
}
private static int getNetVersion() { return 0; }//这里从网络上取得网上的版本
public static void getVersion()
{
int version = proxy.GetNetVersion();
Console.WriteLine(version.toString());
}
}