单击按钮时,我正在尝试在 C# 中的服务器和客户端之间复制文件。我希望使用后台工作人员,因为我不希望副本在复制时使我的表单卡住。我有这个代码:
private void connectBtn_Click(object sender, EventArgs e)
{
if (!isConnected)
{
clientSock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
connectBtn.Text = "Disconnect From Server";
connectBtn.Refresh();
clientSock.Connect(remoteIpAddress, remotePort);
statusColor.BackColor = Color.Green;
statusColor.Refresh();
isConnected = true;
buttonAddDirectory.Enabled = true;
buttonAddFile.Enabled = true;
var bg = new BackgroundWorker();
bg.DoWork += (sender, e) => copyDllsToServer();
bg.RunWorkerAsync();
if (backgroundWorker.IsBusy != true)
backgroundWorker.RunWorkerAsync();
}
}
复制函数是:copyDllsToServer()。
这样做时,我收到以下错误:A local variable named 'sender' cannot be declared in this scope because it would give a different meaning to 'sender', which is already used in a 'parent or current' scope to denote something else
. 我该如何解决?
BackgroundWorker
当多个客户端运行它时,它会正常工作吗?