C#WPF在套接字发送和接收数据C时显示加载屏幕#
我正在构建一个WPF应用程序,它使用套接字在服务器之间传输数据。 问题:当从服务器发送和接收数据时,屏幕被冻结,我想添加一个简单的加载动画,以便最终用户知道它当前正在加载,但我不知道如何加载 我的C#套接字代码:C#WPF在套接字发送和接收数据C时显示加载屏幕#,c#,wpf,sockets,C#,Wpf,Sockets,我正在构建一个WPF应用程序,它使用套接字在服务器之间传输数据。 问题:当从服务器发送和接收数据时,屏幕被冻结,我想添加一个简单的加载动画,以便最终用户知道它当前正在加载,但我不知道如何加载 我的C#套接字代码: public static string SendRecOne(string dataToSvr) { TcpClient client = new TcpClient(SERVER_NAME, PORT); #region SendReques
public static string SendRecOne(string dataToSvr)
{
TcpClient client = new TcpClient(SERVER_NAME, PORT);
#region SendRequest
int ByteCount = Encoding.ASCII.GetByteCount(dataToSvr); //How much bytes?
byte[] ByteBuffer = new byte[1024]; //initialize byte array
ByteBuffer = Encoding.ASCII.GetBytes(dataToSvr);
NetworkStream stream = client.GetStream();
stream.Write(ByteBuffer, 0, ByteBuffer.Length);
#endregion
#region Receive Response
//byte[] responseData = new byte[client.ReceiveBufferSize];
//int bytesRead = stream.Read(responseData, 0, client.ReceiveBufferSize);
int i;
string ToReturn = null;
ByteBuffer = new byte[ByteBuffer.Length];
MemoryStream ms = new MemoryStream();
while (true)
{
if (stream.DataAvailable)
{
while ((i = stream.Read(ByteBuffer, 0, ByteBuffer.Length)) != 0)
{
ms.Write(ByteBuffer, 0, ByteBuffer.Length);
if (stream.DataAvailable)
continue;
else
break;
}
ToReturn = Encoding.ASCII.GetString(ms.ToArray());
return ToReturn;
}
}
#endregion
我在按下窗口中的一个按钮后调用它,它的名字是“login.xaml”,在检查数据是否正确后,它关闭当前窗口并初始化dashboard.xaml。
我只需要在与服务器通信时添加一个动画
谢谢 在后台线程上调用
SendRecOne
方法,或使用*Async重载使其异步:
public static async Task<string> SendRecOne(string dataToSvr)
{
progressBar.Visibility = Visibility.Visible;
string ToReturn = null;
using (TcpClient client = new TcpClient(SERVER_NAME, PORT))
{
int ByteCount = Encoding.ASCII.GetByteCount(dataToSvr); //How much bytes?
byte[] ByteBuffer = new byte[1024]; //initialize byte array
ByteBuffer = Encoding.ASCII.GetBytes(dataToSvr);
NetworkStream stream = client.GetStream();
await stream.WriteAsync(ByteBuffer, 0, ByteBuffer.Length);
//byte[] responseData = new byte[client.ReceiveBufferSize];
//int bytesRead = await stream.ReadAsync(responseData, 0, client.ReceiveBufferSize);
int i;
ByteBuffer = new byte[ByteBuffer.Length];
MemoryStream ms = new MemoryStream();
if (stream.DataAvailable)
{
while ((i = await stream.ReadAsync(ByteBuffer, 0, ByteBuffer.Length)) != 0)
{
await ms.WriteAsync(ByteBuffer, 0, ByteBuffer.Length);
if (!stream.DataAvailable)
break;
}
ToReturn = Encoding.ASCII.GetString(ms.ToArray());
}
progressBar.Visibility = Visibility.Collapsed;
}
return ToReturn;
}
公共静态异步任务SendRecOne(字符串dataToSvr)
{
progressBar.Visibility=可见性.Visibility;
字符串ToReturn=null;
使用(TcpClient client=newtcpclient(服务器名称,端口))
{
int ByteCount=Encoding.ASCII.GetByteCount(dataToSvr);//多少字节?
byte[]ByteBuffer=新字节[1024];//初始化字节数组
ByteBuffer=Encoding.ASCII.GetBytes(dataToSvr);
NetworkStream=client.GetStream();
wait stream.WriteAsync(ByteBuffer,0,ByteBuffer.Length);
//byte[]responseData=新字节[client.ReceiveBufferSize];
//int bytesRead=wait stream.ReadAsync(responseData,0,client.ReceiveBufferSize);
int i;
ByteBuffer=新字节[ByteBuffer.Length];
MemoryStream ms=新的MemoryStream();
if(stream.DataAvailable)
{
while((i=wait stream.ReadAsync(ByteBuffer,0,ByteBuffer.Length))!=0)
{
等待ms.WriteAsync(ByteBuffer,0,ByteBuffer.Length);
如果(!stream.DataAvailable)
打破
}
ToReturn=Encoding.ASCII.GetString(ms.ToArray());
}
progressBar.Visibility=Visibility.collazed;
}
回归回归;
}
XAML:
<ProgressBar x:Name="progressBar" IsIndeterminate="True" />
UI线程无法同时处理消息和执行代码。在后台线程上调用
SendRecOne
方法,或使用*Async重载使其异步:
public static async Task<string> SendRecOne(string dataToSvr)
{
progressBar.Visibility = Visibility.Visible;
string ToReturn = null;
using (TcpClient client = new TcpClient(SERVER_NAME, PORT))
{
int ByteCount = Encoding.ASCII.GetByteCount(dataToSvr); //How much bytes?
byte[] ByteBuffer = new byte[1024]; //initialize byte array
ByteBuffer = Encoding.ASCII.GetBytes(dataToSvr);
NetworkStream stream = client.GetStream();
await stream.WriteAsync(ByteBuffer, 0, ByteBuffer.Length);
//byte[] responseData = new byte[client.ReceiveBufferSize];
//int bytesRead = await stream.ReadAsync(responseData, 0, client.ReceiveBufferSize);
int i;
ByteBuffer = new byte[ByteBuffer.Length];
MemoryStream ms = new MemoryStream();
if (stream.DataAvailable)
{
while ((i = await stream.ReadAsync(ByteBuffer, 0, ByteBuffer.Length)) != 0)
{
await ms.WriteAsync(ByteBuffer, 0, ByteBuffer.Length);
if (!stream.DataAvailable)
break;
}
ToReturn = Encoding.ASCII.GetString(ms.ToArray());
}
progressBar.Visibility = Visibility.Collapsed;
}
return ToReturn;
}
公共静态异步任务SendRecOne(字符串dataToSvr)
{
progressBar.Visibility=可见性.Visibility;
字符串ToReturn=null;
使用(TcpClient client=newtcpclient(服务器名称,端口))
{
int ByteCount=Encoding.ASCII.GetByteCount(dataToSvr);//多少字节?
byte[]ByteBuffer=新字节[1024];//初始化字节数组
ByteBuffer=Encoding.ASCII.GetBytes(dataToSvr);
NetworkStream=client.GetStream();
wait stream.WriteAsync(ByteBuffer,0,ByteBuffer.Length);
//byte[]responseData=新字节[client.ReceiveBufferSize];
//int bytesRead=wait stream.ReadAsync(responseData,0,client.ReceiveBufferSize);
int i;
ByteBuffer=新字节[ByteBuffer.Length];
MemoryStream ms=新的MemoryStream();
if(stream.DataAvailable)
{
while((i=wait stream.ReadAsync(ByteBuffer,0,ByteBuffer.Length))!=0)
{
等待ms.WriteAsync(ByteBuffer,0,ByteBuffer.Length);
如果(!stream.DataAvailable)
打破
}
ToReturn=Encoding.ASCII.GetString(ms.ToArray());
}
progressBar.Visibility=Visibility.collazed;
}
回归回归;
}
XAML:
<ProgressBar x:Name="progressBar" IsIndeterminate="True" />
UI线程不能同时处理消息和执行代码。我会使用异步任务方法来完成。并在上传时显示忙指示灯或进程指示灯。但是,如果您的整个体系结构是同步的,那么它将有点乏味。我想所有这些都是在UI线程上运行的,对吗?首先,你应该使用一个线程来防止窗口在Gerneralife中冻结,如果你只想显示一个加载动画,你可以设置window.cursor=cursors.wati通常将代码放入backgroundworker,这相当于创建一个异步任务。@jdweng在WPF中异步/任务不比BGW更可取吗?对我来说,在MVVM等上下文中使用异步任务感觉更直接。但这可能只是品味的问题,我会用异步任务方法来实现。并在上传时显示忙指示灯或进程指示灯。但是,如果您的整个体系结构是同步的,那么它将有点乏味。我想所有这些都是在UI线程上运行的,对吗?首先,你应该使用一个线程来防止窗口在Gerneralife中冻结,如果你只想显示一个加载动画,你可以设置window.cursor=cursors.wati通常将代码放入backgroundworker,这相当于创建一个异步任务。@jdweng在WPF中异步/任务不比BGW更可取吗?对我来说,在MVVM等上下文中使用异步任务感觉更直接。但这可能只是口味的问题。