在NetworkStream中运行WinForms中的动画

时间:2018-03-04 20:23:54

标签: c# winforms networkstream

我正在编写文件上传软件,我想在文件上传时添加加载动画,以便客户端不会挂起,我创建了一个带动画的新表单。当我显示新表单时,动画可以正常工作,但是当客户端将文件发送到服务器时,它不会移动直到完成。 任何帮助?

代码:

DialogResult result = openFileDialogU.ShowDialog();
if (result == DialogResult.OK && projectList.SelectedIndex > -1)
{
    LoadingForm LoadForm = new LoadingForm();   //The Loading From With The Animation
    LoadForm.Show();
    this.File = openFileDialogU.FileName;
    string fileInfo = this.File.Split('.')[this.File.Split('.').Length - 1];
    string project = projectList.GetItemText(projectList.SelectedItem);
    int lastVersion = Get_lastVersion();
    if (versionList.SelectedItem.ToString() == lastVersion.ToString())
    {
        errorMessage1.Visible = false;
        successMessage1.Set_Message("Project Uploading!");
        successMessage1.Visible = true;
        successMessage1.BringToFront();
        this.cSock.Update_Project(projectList.SelectedItem.ToString(), this.File, fileInfo);    //Sending File To Server Method
    }
    else
    {
        successMessage1.Visible = false;
        errorMessage1.Set_Message("Newer Version Available");
        errorMessage1.Visible = true;
        successMessage1.BringToFront();
        Done = false;
    }

Update_Project方法:

public void Update_Project(string project, string data, string fileInfo)
{
    bool valiable = false;
    foreach (string ext in this.text_extensions)
    {
        if (fileInfo == ext)
        {
            valiable = true;
            break;
        }
    }            
    if (!valiable)
    {
        throw new System.DivideByZeroException();
    }

    NetworkStream serverStream = clientSocket.GetStream();

    byte[] OutStream = System.Text.Encoding.ASCII.GetBytes("Update.");
    serverStream.Write(OutStream, 0, OutStream.Length);
    serverStream.Flush();

    byte[] inStream = new byte[10025];
    serverStream.Read(inStream, 0, inStream.Length);

    OutStream = System.Text.Encoding.ASCII.GetBytes(project.Replace("\0", string.Empty));
    serverStream.Write(OutStream, 0, OutStream.Length);
    serverStream.Flush();

    inStream = new byte[10025];
    serverStream.Read(inStream, 0, inStream.Length);


    FileInfo file = new FileInfo(data);
    string lenghtf = file.Length.ToString();
    OutStream = System.Text.Encoding.ASCII.GetBytes(lenghtf);
    serverStream.Write(OutStream, 0, OutStream.Length);
    serverStream.Flush();

    inStream = new byte[10025];
    serverStream.Read(inStream, 0, inStream.Length);

    OutStream = System.Text.Encoding.ASCII.GetBytes(fileInfo.Replace("\0", string.Empty));
    serverStream.Write(OutStream, 0, OutStream.Length);
    serverStream.Flush();

    inStream = new byte[10025];
    serverStream.Read(inStream, 0, inStream.Length);

    if (fileInfo != "png")
    {
        using (FileStream fs = File.Open(data, FileMode.Open))
        {
            byte[] b = new byte[1024];
            UTF8Encoding temp = new UTF8Encoding(true);
            while (fs.Read(b, 0, b.Length) > 0)
            {
                OutStream = System.Text.Encoding.ASCII.GetBytes(temp.GetString(b));
                serverStream.Write(OutStream, 0, OutStream.Length);
                serverStream.Flush();
            }

        }
    }
    else
    {
        using (var fileStream = File.OpenRead(data))
        {
            fileStream.CopyTo(serverStream);
        }
    }

    inStream = new byte[1025];
    serverStream.Read(inStream, 0, inStream.Length);
}

0 个答案:

没有答案