void webclient_OpenWriteCompleted(object sender,OpenWriteCompletedEventArgs e)
        {
           
            Stream inputStream = e.UserState as Stream;
 while((rfsReciveBytesLen = inputStream.Read(rfsReciveBytesBuffer, 0, rfsReciveBytesBuffer.Length)) > 0)
            {             }
}原来是将读文件的操作,,放在异步事件里完成的,,发现不能同步更新 进度条想将上面异步事件的处理过程,放button 事件里来做
 
发现 这句: Stream inputStream = e.UserState as Stream; 在按钮事件里报错 请问在按钮里该如何声明呢?
 private void button4_Click(object sender, RoutedEventArgs e)
        {
 Stream inputStream=null ; 我直接这样 不报错
 但是走到下面时报错了while((rfsReciveBytesLen = inputStream.Read(rfsReciveBytesBuffer, 0, rfsReciveBytesBuffer.Length)) > 0)
    
}
该如何处理恩?谢谢

解决方案 »

  1.   

    inputStream=null当然会报错,Stream inputStream = e.UserState as Stream当然也会错,因为e没有UserState属性,
      

  2.   

    inputStream = e.UserState as Stream;可以放在原来的地方,比如你的异步事件里,但Stream inputStream不能是局部变量,而是一个类变量,放在form里,在button事件中可以访问,不过你还要解决一个问题,inputStream 不能在按下button时关闭,
      

  3.   

    你在button读取流是不好的做法,这样很难可靠的关闭inputStream流,
      

  4.   

    晕死 有这么多问题呀
    ///////////////////
    你先调试,看报错原因是什么,异步怎么不能更新进度条呢,线程+委托没问题
    错误:
    " 用户代码未处理 nullReferenceException"
    ///////////////////
    你先调试,看报错原因是什么,异步怎么不能更新进度条呢,线程+委托没问题 委托不会用 呵呵 
      

  5.   


    异步时,不能同步更新 进度条的void webclient_OpenWriteCompleted(object sender,OpenWriteCompletedEventArgs e)
     主要代码如下:
     
     while((rfsReciveBytesLen = inputStream.Read(rfsReciveBytesBuffer, 0, rfsReciveBytesBuffer.Length)) > 0)
                {
                    //rfsSendResetEvent.Reset();
                    if (rfsReciveBytesLen == rfsReciveBytesBuffer.Length)
                    {
                        Array.ConstrainedCopy(rfsReciveBytesBuffer, 0, rfsSendBytesBuffer, 3, arrayLen);                    
                    }
                    else 
                    {                  
                        Array.Resize(ref rfsSendBytesBuffer,rfsReciveBytesLen+4);
                        rfsSendBytesBuffer[1] = (byte)(rfsReciveBytesLen+2);
                        rfsSendBytesBuffer[rfsReciveBytesLen+3] = 0x01;
                        Array.ConstrainedCopy(rfsReciveBytesBuffer, 0, rfsSendBytesBuffer, 3, rfsReciveBytesLen);                  
                    }
                    rfsSendCommand(rfsSendBytesBuffer);
                    Thread.Sleep(1000);                
                    //rfsSendResetEvent.WaitOne(10);
                    updataProgressStatus();
                    //rfsSendResetEvent.Set();
                    
                }
                上面代码的工作是,每次读一个文件固定长度,比如100字节,然后发送
                我现在想要的效果是,一个进度条,提示完成了多少状态
                progressBar1.Value的初始值为0
                progressBar1.Value的最大值为文件的大小
                
                下面的代码为 progressBar1.Value每次加132字节,,因为一次读取文件132字节,并发送。
                
                现在的情况是:
                progressBar1的提示条,没有动态的变化,,而且在文件都读取并发送完成后,瞬间用深色填满了progressBar1
                要怎么弄,才能动态的改变提示状态呢?
                
    void updataProgressStatus()
            {
                this.Dispatcher.BeginInvoke(() =>
                {
                    progressBar1.Value = progressBar1.Value + 132;
                    
                });
            }
      

  6.   

    这个简单,在你的updataProgressStatus里面加上一句Application.DoEvents就行了,void updataProgressStatus()
       {
       this.Dispatcher.BeginInvoke(() =>
       {
       progressBar1.Value = progressBar1.Value + 132;Application.DoEvents();
         
      });
       }
      

  7.   

    错误 3 “System.Windows.Application”并不包含“DoEvents”的定义 错误 2 只有 assignment、call、increment、decrement 和 new 对象表达式可用作语句
    加了不行呀
      

  8.   

    原来你用的是WPF,那就麻烦了,
      

  9.   

    呵呵
    是对,,非常感谢 你的热心帮助 用了个最笨的办法 用label 提示,,