我在一个线程A中放了一个子线程B
但是不知道为什么,线程B 始终只执行了一点点就再也不执行了,哪位老大帮忙看看哦 #region 响音服务器的消息
/// </summary>
private void ServerResponse()
{
byte[] buff=new byte[1024];
int len;
try
{
if(!Strm.CanRead)
return;
//用循环不断的与服务器交互
while(true)
{
len=Strm.Read(buff,0,buff.Length);
msg=System.Text.Encoding.UTF8.GetString(buff,0,len);
msg.Trim();
string[] tokens=msg.Split(new char[]{'|'});
if(tokens[0]=="WSEND")//对方想发送文件给你
{
sendstart=true;
sender1=tokens[1];
senderIP=tokens[2];
senderPort=tokens[3];
string s=tokens[4].ToString();
this.saveFileDialog1.FileName=tokens[5].ToString();
this.richTextBox1.Text+="\n"+tokens[4].Trim()+"";

// tokens[0]="";
this.menuItem2.Visible=true;
this.menuItem3.Visible=true;

}
if(tokens[0]=="WGET")//对方同意接收文件
{
this.richTextBox1.Text+="\n"+tokens[4].Trim()+"";
this.label1.Visible=true;
this.progressBar1.Visible=true;
IPEndPoint ipep=new IPEndPoint(IPAddress.Any,int.Parse(localPort));//创建一个网络端点
server=new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);//创建一个套接字
server.Bind(ipep);//绑定套接字到端口
server.Listen(10);//开始侦听(并堵塞该线程
TempThread=new Thread(new ThreadStart(this.startsend));
TempThread.Start();
// TempThread.Join();
string message="START|"+UserAlias+"|"+UserGet+"|";
Byte[] outbytes=System.Text.Encoding.UTF8.GetBytes(message.ToCharArray());
Strm.Write(outbytes,0,outbytes.Length);//发送准备信息

this.caninsert=false;
// TempThread.Abort();
}

if(tokens[0]=="QUIT")
{
break;
}
if(tokens[0]=="READY")
{

Thread TempThread=new Thread(new ThreadStart(this.StartReceive));
TempThread.Start();
}
}
Application.ExitThread();
}
catch (Exception e)
{

MessageBox.Show(e.Message);

}
}

解决方案 »

  1.   

    #region 发送文件
    private void startsend()
    {

    try
    {

    if(fullname!=null)
    {
    client=server.Accept();//获得客户端节点对象
    FileInfo fo = new FileInfo(fullname);
    FileStream fs = fo.OpenRead();
    int packetsize= 500;//包的尺寸
    int packetcount = (int)(fs.Length/(long)packetsize);//包的数量
    this.progressBar1.Maximum=packetcount;
    int LastDataPacket = (int)(fs.Length-((long)(packetsize*packetcount))); //最后一个包的大小

    IPEndPoint clientep=(IPEndPoint)client.RemoteEndPoint;//获得客户端的IP地址

    PTP.EzoneModule.SendVarData(client,System.Text.Encoding.Unicode.GetBytes(fo.Name)); //发送[文件名]到客户端 PTP.EzoneModule.SendVarData(client,System.Text.Encoding.Unicode.GetBytes(packetsize.ToString()));//发送[包的大小]到客户端 PTP.EzoneModule.SendVarData(client,System.Text.Encoding.Unicode.GetBytes(packetcount.ToString()));//发送[包的总数量]到客户端

    PTP.EzoneModule.SendVarData(client,System.Text.Encoding.Unicode.GetBytes(LastDataPacket.ToString()));//发送[最后一个包的大小]到客户端 //数据包
    byte[]data=new byte[packetsize];
    //开始循环发送数据包
    try
    {
    for(int i=0;i<packetcount;i++)
    {
    //从文件流读取数据并填充数据包
    fs.Read(data,0,data.Length);
    //发送数据包
    PTP.EzoneModule.SendVarData(client,data);
    //显示发送数据包的个数
    // this.textBox10.Text=((int)(i+1)).ToString();
    //进度条值的显示
    this.progressBar1.PerformStep();
    }
    if(LastDataPacket!=0)  
    //如果还有多余的数据包,则应该发送完毕!
    {
    data=new byte[LastDataPacket];
    fs.Read(data,0,data.Length);
    PTP.EzoneModule.SendVarData(client,data);
    this.progressBar1.Value=this.progressBar1.Maximum;
    // this.progressBar1.PerformStep();
    }
    //关闭套接字
    client.Shutdown(SocketShutdown.Both);
    client.Close();
    server.Close();
    //关闭文件流
    fs.Close();
    this.menuItem6.Visible=false;
    this.menuItem1.Enabled=true;
    this.caninsert=true;
    sendstart=false;

    }
    catch(Exception e)
    {
    //关闭文件流
    this.caninsert=true;
    sendstart=false;
    MessageBox.Show(e.Message);
    MessageBox.Show(UserAlias);
    }

    }
    else
    {
    this.caninsert=true;
    sendstart=false;
    MessageBox.Show("");
    }
    }
    catch(Exception e)
    {
    this.caninsert=true;
    MessageBox.Show(e.Message);
    sendstart=false;

    }
    }
    #endregion
    #region 开始接收文件
    private void StartReceive()
    {
    try
    {
    //指向远程服务端节点
    IPEndPoint ipep=new IPEndPoint(IPAddress.Parse(senderIP),int.Parse(senderPort));
    //创建套接字
    clientRecive=new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
    //连接到发送端
    clientRecive.Connect(ipep);
    //获得[文件名]
    string SendFileName=System.Text.Encoding.Unicode.GetString(PTP.EzoneModule.ReceiveVarData(clientRecive));
    // this.textBox2.Text=SendFileName;
    //获得[包的大小]
    string pocketbig=System.Text.Encoding.Unicode.GetString(PTP.EzoneModule.ReceiveVarData(clientRecive));
    //获得[包的总数量]
    string lastpacket=System.Text.Encoding.Unicode.GetString(PTP.EzoneModule.ReceiveVarData(clientRecive));
    this.progressBar1.Maximum=int.Parse(lastpacket);
    //获得[最后一个包的大小]
    string pocketbigsize=System.Text.Encoding.Unicode.GetString(PTP.EzoneModule.ReceiveVarData(clientRecive));
    //创建一个新文件
    FileStream MyFileStream=new FileStream(this.saveFileDialog1.FileName,FileMode.Create,FileAccess.Write);
    //已发送包的个数
    int SendedCount=0;
    while(listen1)
    {
    byte[]data=PTP.EzoneModule.ReceiveVarData(clientRecive);
    if(data.Length==0)
    {
    break;
    }
    else
    {
    SendedCount++;
    //将接收到的数据包写入到文件流对象

    // MyFileStream.Position=System.Convert.ToInt64(this.saveFileDialog1.FileName);
    MyFileStream.Write(data,0,data.Length);
    //显示已发送包的个数
    //进度条值的显示
    this.progressBar1.PerformStep();
    }
    }
    this.progressBar1.Value=this.progressBar1.Maximum;
    //关闭文件流
    MyFileStream.Close();
    //关闭套接字
    clientRecive.Close();
    string message="FINISH|"+UserAlias+"|"+sender1+"|";
    Byte[] outbytes=System.Text.Encoding.UTF8.GetBytes(message.ToCharArray());
    Strm.Write(outbytes,0,outbytes.Length);
    this.menuItem2.Visible=false;
    this.menuItem3.Visible=false;
    this.label1.Visible=false;
    this.progressBar1.Visible=false;
    message="";
    sendstart=false;
    this.caninsert=true;
    }

    catch(Exception e)
    {
    this.caninsert=true;
    sendstart=false;
    MessageBox.Show(e.Message);
    MessageBox.Show(UserAlias);
    }
    }
    #endregion
      

  2.   

    去掉
    Application.ExitThread();
    这一句
      

  3.   

    没有用哦~~,根本没有执行到这句哦, #region 响音服务器的消息 这段是一直不停的循环的,只有程序关闭了才回执行到Application.ExitThread();
      

  4.   

    应该说是发送文件的端执行到this.progressBar1.Maximum=packetcount;这句就不执行下去了 ,为什么哦
      

  5.   

    to 应该说是发送文件的端执行到this.progressBar1.Maximum=packetcount;这句就不执行下去了 要想控制窗体上的控件,直接操作是不行的,参看
    http://blog.csdn.net/knight94/archive/2006/03/16/626584.aspx
      

  6.   

    老大,能不能给我个MSN的说,我还有几个问题想请教