我已经定义了一个后台工作程序,它在另一个线程中处理网络连接,每当它调用WNetUseConnection时(为了访问共享文件夹),它都会完全阻止应用程序,无论该函数是否在另一个线程中运行。我假设它必须是一个系统调用,它会在函数返回之前停止任何任务,但我想知道是否有任何方法可以在不阻止应用程序的情况下执行此操作
背景工作者代码:
private void Login()
{
if (bwWorker.IsBusy != true)
{
ErrorPanel.Visibility = Visibility.Collapsed;
bwWorker.RunWorkerAsync();
}
}
private void BackGroundWorkerLogin(object sender, DoWorkEventArgs e)
{
var credentials = new NetworkCredential(
(string)Application.Current.Properties["FileSystemUserId"],
(string)Application.Current.Properties["FileSystemUserPassword"]);
using (var netConnection = new NetworkConnection((string)Application.Current.Properties["FileSystemPath"], credentials))
{
e.Result = netConnection.ConnectionResult;
}
}
private void BackGroundWorkerLoginCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if ((int)e.Result != 0)
{
}
}
NetworkConnection代码:
public NetworkConnection(string netName, NetworkCredential credentials)
{
networkName = netName;
var netResource = new NetResource
{
Scope = ResourceScope.GlobalNetwork,
ResourceType = ResourceType.Disk,
DisplayType = ResourceDisplaytype.Share,
RemoteName = networkName
};
var userName = string.IsNullOrEmpty(credentials.Domain)
? credentials.UserName
: string.Format(@"{0}\{1}", credentials.Domain, credentials.UserName);
var result = WNetUseConnection(
IntPtr.Zero,
netResource,
credentials.Password,
userName,
0, null, null, null);
ConnectionResult = result;
}