当前位置:编程学习 > C#/ASP.NET >>

请教 线程间,线程内,资源互斥问题...

线程函数,以此函数开多个线程 
void   thread_body() 

        //初始工作 
        while() 
        { 
                  //写文件a.xml 
        } 


怎么能够解决好a.xml的访问问题?? 
线程内的循环调用....不好解决啊 
整了我两天了,这问题,还是不能完全解决互斥 
线程间,线程内...都要解决,有没好办法? --------------------编程问答-------------------- 1 你的设计本身就有问题了,多个线程同时对同一个内核对象(这里是你的这个xml文件)进行独占操作(写),不可避免是要等待的了。

2 如果你要这么做的话建议给写文件的过程加一个队列。

3 以后提问最后说清楚自己的问题,你只说解决 访问问题而没有说你访问出了什么问题(死锁? 性能问题?还是异常?还是其他什么问题)线程内循环调用不好解决?解决什么? 什么叫解决互斥?是想互斥还是想不互斥?  --------------------编程问答-------------------- 文件“C:\a.xml”正由另一进程使用,因此该进程无法访问该文件

总是出现这错误 --------------------编程问答-------------------- syeerzy 
怎么给定文件的过程加队列啊? --------------------编程问答-------------------- 打开此文件前锁定.
读完或写完解锁
不可能两个都在读和写的. --------------------编程问答-------------------- 我是想在多线程下载的同时,记录每块的下载位置,以便断点续传,像下面这样,可以实现两个线程, 
而其它线程均在等待.资源都由这两个线程交换使用,奇怪,怎么可能两个线程? 


public   void   DownThread() 
                { 
                        //初始化线程所需参数 
                        this.InitThreadParameter(); 
                        HttpWebRequest   request   =   null; 
                        Stream   httpStream   =   null; 
                        try 
                        { 
                                //连接 
                                request   =   (HttpWebRequest)WebRequest.Create(new   Uri(this.m_requestUrl)); 
                                //请求数据范围 
                                request.AddRange((int)this.m_rangeStart[this.m_threadId],   (int)this.m_rangeEnd[this.m_threadId]); 
                                HttpWebResponse   response   =   (HttpWebResponse)request.GetResponse(); 
                                httpStream   =   response.GetResponseStream(); 
                                //写入文件 
                                byte[]   byteBuf   =   new   byte[512]; 

                                int   readByte   =   httpStream.Read(byteBuf,   0,   512); 

                                //定位文件写入位置 
                                this.m_fs.Seek(this.m_rangeStart[this.m_threadId],   SeekOrigin.Begin); 
                                while   (readByte   >   0) 
                                { 
                                        Monitor.Enter(MutilDownLoad.s_syncObject); 

                                        //写入文件 
                                        this.m_fs.Write(byteBuf,   0,   readByte); 

                                        //本块完成下载量增加readByte字节 
                                        this.m_blockCompleteLenthArray[this.m_threadId]   =   (long)readByte; 
                                    //下载总大小增加readByte字节 
                                        this.m_completeLenth   =   (long)readByte; 
                                    //更新下载列表中本下载对象的已下载量,即写xml文件
                                        this.m_status   =   new   DownLoadStatus(this.m_fileInfoLoad,   this.m_requestUrl, 
                                                                                                                        this.m_savePath,   this.m_threads,     this.m_completeLenth, 
                                                                                                                        this.m_usedTime,   this.m_blockCompleteLenthArray); 

                                        this.m_status.UpdateStatus(this.m_threadId); 
                  
                                        Monitor.Exit(MutilDownLoad.s_syncObject); 
                                      
                                        Thread.Sleep(300); 
                                        //继续读取数据流 
                                            readByte   =   httpStream.Read(byteBuf,   0,   512); 

                                } 

                                //关闭文件流 
                                this.m_fs.Close(); 

                        } 
                        catch   (WebException   webEx) 
                        { 
                                throw   webEx; 
                        } 
                        catch   (HttpListenerException   httpListenerEx) 
                        { 
                                throw   httpListenerEx; 
                        } 
                        catch   (IOException   ioEx) 
                        { 
                                throw   ioEx; 
                        } 
                        catch   (Exception   ex) 
                        { 
                                throw   ex; 
                        } 
                        finally 
                        {   
                                request.Abort(); 
                        } 
                }
补充:.NET技术 ,  C#
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,