FtpWebRequest 上传文件 想停止上传怎么做?

ljp88888 2012-02-01 09:28:35
我用FtpWebRequest 类做了一个ftp的上传程序, 现在想实现上传一半的时候 停止上传 怎么做啊 我创建了一个线程进行上传的 防止线程阻塞
...全文
233 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
s346663833 2014-10-27
  • 打赏
  • 举报
回复
表示二楼说的就非常好...
weisoso 2014-02-08
  • 打赏
  • 举报
回复
朕赐你肥皂 2012-02-09
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 ljp88888 的回复:]

引用 13 楼 gaind 的回复:

不是和我的方法一样么,一个是用全局变量去标示,10#的是用属性去标示



嗯 跟你的方法一样 但是我还是没会做 我点完了 是暂停了 但是释放不了线程 关闭不了
[/Quote]


一般中止线程的方法有2种,一种就是我提到的线程中循环全局变量来确定是否退出线程,这个是结束线程比较好的方法
第二种就是Abort()方法


你说的释放不了线程,关闭不了,其实是因为中止线程后,系统需要一定时间去清理代码!你可以在主线程中调用子线程的JOIN方法来解决,JOIN方法中指定主线程等待子线程结束的等待时间
ViewStates 2012-02-03
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 ljp88888 的回复:]

引用 13 楼 gaind 的回复:

不是和我的方法一样么,一个是用全局变量去标示,10#的是用属性去标示



嗯 跟你的方法一样 但是我还是没会做 我点完了 是暂停了 但是释放不了线程 关闭不了
[/Quote]
什么叫释放不了线程?
ljp88888 2012-02-02
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 gaind 的回复:]

不是和我的方法一样么,一个是用全局变量去标示,10#的是用属性去标示
[/Quote]


嗯 跟你的方法一样 但是我还是没会做 我点完了 是暂停了 但是释放不了线程 关闭不了
朕赐你肥皂 2012-02-02
  • 打赏
  • 举报
回复
不是和我的方法一样么,一个是用全局变量去标示,10#的是用属性去标示
ljp88888 2012-02-02
  • 打赏
  • 举报
回复
太专业了,有些看不太明白
ljp88888 2012-02-02
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 yulu380393306 的回复:]

首先定义上传文件的Model
C# code

class UploadFile
{
Public string FileSize
{
get;set
}

Public bool Percent
{
get;set
}
//默认为false
Public bool IsUploadCancel
{
ge……
[/Quote]

谢谢
ljp88888 2012-02-01
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 gaind 的回复:]

我有个办法,搞个全局变量
bool Flag = true;
判断Flag的值,只有true的时候才上传到服务器的一个临时文件夹
然后当你点取消上传的时候Falg=false;那么删除那个临时文件夹
如果Flag一直为true的话,那么就将临时文件夹的内容copy到你上传的文件夹,然后删除临时文件夹
[/Quote]

你这个方法只是不传输了,但是线程还在 删除不了的
朕赐你肥皂 2012-02-01
  • 打赏
  • 举报
回复
我有个办法,搞个全局变量
bool Flag = true;
判断Flag的值,只有true的时候才上传到服务器的一个临时文件夹
然后当你点取消上传的时候Falg=false;那么删除那个临时文件夹
如果Flag一直为true的话,那么就将临时文件夹的内容copy到你上传的文件夹,然后删除临时文件夹
ljp88888 2012-02-01
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 viewstates 的回复:]

建议在你接受停止上传指令时将一标志位更改,在上传线程对应的委托方法中周期性判断此标志位,如果标志位复位了就跳出上传过程。
建议不要在线程外强行ABORT线程,这样可能会导致线程抛出一个THREADABORTEXCEPTION
[/Quote]

我可能打错字了, 我的停止 不是暂停的意思, 是取消上传 关闭释放掉线程 怎么弄啊
ljp88888 2012-02-01
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 chinajiyong 的回复:]

有上传线程,你想停止上传就停了上传线程。
C# code

if( thredDownload.ThreadState == System.Threading.ThreadState.Running )
//停止上传线程thredDownload,最好设置一个停止标志
[/Quote]

我可能打错字了, 我的停止 不是暂停的意思, 是取消上传 关闭释放掉线程 怎么弄啊
ViewStates 2012-02-01
  • 打赏
  • 举报
回复
建议在你接受停止上传指令时将一标志位更改,在上传线程对应的委托方法中周期性判断此标志位,如果标志位复位了就跳出上传过程。
建议不要在线程外强行ABORT线程,这样可能会导致线程抛出一个THREADABORTEXCEPTION
EnForGrass 2012-02-01
  • 打赏
  • 举报
回复
有上传线程,你想停止上传就停了上传线程。

if( thredDownload.ThreadState == System.Threading.ThreadState.Running )
//停止上传线程thredDownload,最好设置一个停止标志

yulu380393306 2012-02-01
  • 打赏
  • 举报
回复
首先定义上传文件的Model

class UploadFile
{
Public string FileSize
{
get;set
}

Public bool Percent
{
get;set
}
//默认为false
Public bool IsUploadCancel
{
get;set
}
}

只简单列了几个属性,在上传文件时,有个读取文件的过程 while(true){ //在这儿可以判断IsUploadCancel是否为true},在这个过程中,利用类的引用类型的性质,若有取消按钮,在这个按钮方法中将该类的IsUploadCancel置为true, 这时读取文件那儿就可以判断到IsUploadCancel的值已被修改,这时就可以break了,手写的,可能有些单词错了
ViewStates 2012-02-01
  • 打赏
  • 举报
回复
下面是MSDN的例子,你可以在异步对应的委托方法EndGetStreamCallback中进行我所提到的判断(判断是否被用户终止上传),如果判断出来有用户终止请求则可以跳出循环进入资源清理的步骤(如果有的话)。
当线程对应的委托方法执行完后线程自然就没了


using System;
using System.Net;
using System.Threading;

using System.IO;
namespace Examples.System.Net
{
public class FtpState
{
private ManualResetEvent wait;
private FtpWebRequest request;
private string fileName;
private Exception operationException = null;
string status;

public FtpState()
{
wait = new ManualResetEvent(false);
}

public ManualResetEvent OperationComplete
{
get {return wait;}
}

public FtpWebRequest Request
{
get {return request;}
set {request = value;}
}

public string FileName
{
get {return fileName;}
set {fileName = value;}
}
public Exception OperationException
{
get {return operationException;}
set {operationException = value;}
}
public string StatusDescription
{
get {return status;}
set {status = value;}
}
}
public class AsynchronousFtpUpLoader
{
// Command line arguments are two strings:
// 1. The url that is the name of the file being uploaded to the server.
// 2. The name of the file on the local machine.
//
public static void Main(string[] args)
{
// Create a Uri instance with the specified URI string.
// If the URI is not correctly formed, the Uri constructor
// will throw an exception.
ManualResetEvent waitObject;

Uri target = new Uri (args[0]);
string fileName = args[1];
FtpState state = new FtpState();
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(target);
request.Method = WebRequestMethods.Ftp.UploadFile;

// This example uses anonymous logon.
// The request is anonymous by default; the credential does not have to be specified.
// The example specifies the credential only to
// control how actions are logged on the server.

request.Credentials = new NetworkCredential ("anonymous","janeDoe@contoso.com");

// Store the request in the object that we pass into the
// asynchronous operations.
state.Request = request;
state.FileName = fileName;

// Get the event to wait on.
waitObject = state.OperationComplete;

// Asynchronously get the stream for the file contents.
request.BeginGetRequestStream(
new AsyncCallback (EndGetStreamCallback),
state
);

// Block the current thread until all operations are complete.
waitObject.WaitOne();

// The operations either completed or threw an exception.
if (state.OperationException != null)
{
throw state.OperationException;
}
else
{
Console.WriteLine("The operation completed - {0}", state.StatusDescription);
}
}
private static void EndGetStreamCallback(IAsyncResult ar)
{
FtpState state = (FtpState) ar.AsyncState;

Stream requestStream = null;
// End the asynchronous call to get the request stream.
try
{
requestStream = state.Request.EndGetRequestStream(ar);
// Copy the file contents to the request stream.
const int bufferLength = 2048;
byte[] buffer = new byte[bufferLength];
int count = 0;
int readBytes = 0;
FileStream stream = File.OpenRead(state.FileName);
do
{
readBytes = stream.Read(buffer, 0, bufferLength);
requestStream.Write(buffer, 0, readBytes);
count += readBytes;
}
while (readBytes != 0);
Console.WriteLine ("Writing {0} bytes to the stream.", count);
// IMPORTANT: Close the request stream before sending the request.
requestStream.Close();
// Asynchronously get the response to the upload request.
state.Request.BeginGetResponse(
new AsyncCallback (EndGetResponseCallback),
state
);
}
// Return exceptions to the main application thread.
catch (Exception e)
{
Console.WriteLine("Could not get the request stream.");
state.OperationException = e;
state.OperationComplete.Set();
return;
}

}

// The EndGetResponseCallback method
// completes a call to BeginGetResponse.
private static void EndGetResponseCallback(IAsyncResult ar)
{
FtpState state = (FtpState) ar.AsyncState;
FtpWebResponse response = null;
try
{
response = (FtpWebResponse) state.Request.EndGetResponse(ar);
response.Close();
state.StatusDescription = response.StatusDescription;
// Signal the main application thread that
// the operation is complete.
state.OperationComplete.Set();
}
// Return exceptions to the main application thread.
catch (Exception e)
{
Console.WriteLine ("Error getting response.");
state.OperationException = e;
state.OperationComplete.Set();
}
}
}
}

ljp88888 2012-02-01
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 fengyarongaa 的回复:]

断点续传 socket可以控制
[/Quote]
不是断点续传 是停止 不想传了, 关闭传输 释放掉线程 关闭这个线程
ycproc 2012-02-01
  • 打赏
  • 举报
回复
断点续传 socket可以控制

110,546

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧