4个回答
展开全部
异步就是在发生某件事的时候 “自动” 开一个线程去调用注册的委托。所以,你这个问题分2种情况。
一:事件已发生,另一个线程已开启,这种情况,中止异步可以中止当前线程(Thread.CurrentThread.Abort()),也可以直接return退出那个委托啊魂淡!
二:事件未发生,线程还未开启,这种情况下...你想结束什么?直接关掉那个异步对象不就是了!
一:事件已发生,另一个线程已开启,这种情况,中止异步可以中止当前线程(Thread.CurrentThread.Abort()),也可以直接return退出那个委托啊魂淡!
二:事件未发生,线程还未开启,这种情况下...你想结束什么?直接关掉那个异步对象不就是了!
更多追问追答
追问
那是在异步内部可以终止,我是说外部,比如异步开始了,然后我在界面上点一个按钮就让它终止
追答
我说的是异步的中止原理,如何从外部干涉到内部,这种事情还要我来说了吗?
2012-09-26
展开全部
精彩答案,不对,异步是无法终止的,必须等待执行完毕!因为你无法确定他的位置!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
using System;
using System.Net;
using System.Threading;
using System.Text;
using System.IO;
//Request 类用于通过
//异步调用传递数据
public class RequestState
{
const int BUFFER_SIZE = 1024;
public StringBuilder RequestData;
public byte[] BufferRead;
public HttpWebRequest Request;
public Stream ResponseStream;
//创建适当编码类型的解码器
public Decoder StreamDecode = Encoding.UTF8.GetDecoder();
public RequestState()
{
BufferRead=new byte[BUFFER_SIZE];
RequestData = new StringBuilder("");
Request = null;
ResponseStream = null;
}
}
//ClientGetAsync发出异步请求
class ClientGetAsync
{
public static ManualResetEvent allDone=new ManualResetEvent(false);
const int BUFFER_SIZE = 1024;
public static void Main(string[] args)
{
if (args.Length < 1)
{
showusage();
return;
}
//从命令行获取URI
Uri HttpSite = new Uri(args[0]);
//创建请求对象
HttpWebRequest wreq = (HttpWebRequest)WebRequest.Create(HttpSite);
//创建状态对象
RequestState rs = new RequestState();
//将请求添加到状态,以便它可以被来回传递
rs.Request = wreq;
//发出异步请求
IAsyncResult r = (IAsyncResult)wreq.BeginGetResponse(new AsyncCallback(RespCallback),rs);
//将ManualResetEvent 设置为Wait
//以便在调用回调前,应用程序不退出
allDone.WaitOne();
}
public static void showusage()
{
Console.WriteLine("尝试获取(GET)一个URL");
Console.WriteLine("\r\n用法::");
Console.WriteLine("ClientGetAsync URL");
Console.WriteLine("示例::");
Console.WriteLine("ClientGetAsync http://www.microsoft.con/net/");
}
private static void RespCallback(IAsyncResult ar)
{
//从异步结果获取RequestState对象
RequestState rs = (RequestState)ar.AsyncState;
//从RequestState获取HttpWebRequest
HttpWebRequest req = rs.Request;
//调用EndGetResponse生成HttpWebResponse对象
//该对象来自上面发出的请求
HttpWebResponse resp = (HttpWebResponse)req.EndGetResponse(ar);
//既然我们拥有了响应,就该从
//响应流开始读取数据了
Stream ResponseStream = resp.GetResponseStream();
//该读取操作也使用异步完成,所以,
//我们将以RequestState存储流
rs.ResponseStream = ResponseStream;
//rs.BufferRead 被传入到BeginRead.
//这是数据将被读入的位置
IAsyncResult iarRead = ResponseStream.BeginRead(rs.BufferRead,0,BUFFER_SIZE,new AsyncCallback(ReadCallBack),rs);
}
private static void ReadCallBack(IAsyncResult asyncResult)
{
//从asyncresult获取RequestState对象
RequestState rs = (RequestState)asyncResult.AsyncState;
//取出在RespCallback中设置的ResponseStream
Stream responseStream = rs.ResponseStream;
//此时,rs.BufferRead中应该有一些数据
//读取操作将告诉我们那里是否有数据
int read = responseStream.EndRead(asyncResult);
if (read > 0)
{
//准备Char 数组缓冲区,用于向Unicode转换
Char[] charBuffer = new Char[BUFFER_SIZE];
//将字节流转换为Char 数组,然后转换为字符串
//len显示多少字符被转换为Unicode
int len = rs.StreamDecode.GetChars(rs.BufferRead, 0, read, charBuffer, 0);
String str = new String(charBuffer, 0, len);
rs.RequestData.Append(str);
IAsyncResult ar = responseStream.BeginRead(rs.BufferRead, 0, BUFFER_SIZE, new AsyncCallback(ReadCallBack), rs);
}
else
{
if (rs.RequestData.Length > 1)
{
// 所有数据都已被读取,因此将其显示到控制台
string strContent;
strContent = rs.RequestData.ToString();
Console.WriteLine(strContent);
}
//关闭响应流
responseStream.Close();
allDone.Set();
}
return;
}
}
2. 判断控件的Handle是在当前线程中,则无需异步执行,否则要异步
if (this.gcDC.InvokeRequired)
{
OnRegionChangedDelegate dlgt = new OnRegionChangedDelegate(OnLevelRegionChanged);
this.Invoke(dlgt, new object[] { sender, ev });
}
3. 开启新线程
可以通过开启新线程来执行异步操作,并给异步操作方法传递参数。参数通常包括异步方法所需的所有数据。
Thread thread=new Thread(new ParameterizedThreadStart(GetDataFromWebService));
private static void GetDataFromWebService(object entity){
showWindow();
getData();
window.clost();
}
4.在界面线程外打开程序
resultsCollection.AddRange(entity.DataCol);
InsertIntoTable(resultsCollection, "", entity.tableName, entity.Schema, "", entity.IsNewFetch);
if (window != null)
{
#region[Close progess window]
System.Windows.Application.Current.Dispatcher.Invoke(new Action(() =>
{
(window.WpfUIElement as System.Windows.Window).Close();
entity.SetNexValue();
}));
#endregion
}
using System.Net;
using System.Threading;
using System.Text;
using System.IO;
//Request 类用于通过
//异步调用传递数据
public class RequestState
{
const int BUFFER_SIZE = 1024;
public StringBuilder RequestData;
public byte[] BufferRead;
public HttpWebRequest Request;
public Stream ResponseStream;
//创建适当编码类型的解码器
public Decoder StreamDecode = Encoding.UTF8.GetDecoder();
public RequestState()
{
BufferRead=new byte[BUFFER_SIZE];
RequestData = new StringBuilder("");
Request = null;
ResponseStream = null;
}
}
//ClientGetAsync发出异步请求
class ClientGetAsync
{
public static ManualResetEvent allDone=new ManualResetEvent(false);
const int BUFFER_SIZE = 1024;
public static void Main(string[] args)
{
if (args.Length < 1)
{
showusage();
return;
}
//从命令行获取URI
Uri HttpSite = new Uri(args[0]);
//创建请求对象
HttpWebRequest wreq = (HttpWebRequest)WebRequest.Create(HttpSite);
//创建状态对象
RequestState rs = new RequestState();
//将请求添加到状态,以便它可以被来回传递
rs.Request = wreq;
//发出异步请求
IAsyncResult r = (IAsyncResult)wreq.BeginGetResponse(new AsyncCallback(RespCallback),rs);
//将ManualResetEvent 设置为Wait
//以便在调用回调前,应用程序不退出
allDone.WaitOne();
}
public static void showusage()
{
Console.WriteLine("尝试获取(GET)一个URL");
Console.WriteLine("\r\n用法::");
Console.WriteLine("ClientGetAsync URL");
Console.WriteLine("示例::");
Console.WriteLine("ClientGetAsync http://www.microsoft.con/net/");
}
private static void RespCallback(IAsyncResult ar)
{
//从异步结果获取RequestState对象
RequestState rs = (RequestState)ar.AsyncState;
//从RequestState获取HttpWebRequest
HttpWebRequest req = rs.Request;
//调用EndGetResponse生成HttpWebResponse对象
//该对象来自上面发出的请求
HttpWebResponse resp = (HttpWebResponse)req.EndGetResponse(ar);
//既然我们拥有了响应,就该从
//响应流开始读取数据了
Stream ResponseStream = resp.GetResponseStream();
//该读取操作也使用异步完成,所以,
//我们将以RequestState存储流
rs.ResponseStream = ResponseStream;
//rs.BufferRead 被传入到BeginRead.
//这是数据将被读入的位置
IAsyncResult iarRead = ResponseStream.BeginRead(rs.BufferRead,0,BUFFER_SIZE,new AsyncCallback(ReadCallBack),rs);
}
private static void ReadCallBack(IAsyncResult asyncResult)
{
//从asyncresult获取RequestState对象
RequestState rs = (RequestState)asyncResult.AsyncState;
//取出在RespCallback中设置的ResponseStream
Stream responseStream = rs.ResponseStream;
//此时,rs.BufferRead中应该有一些数据
//读取操作将告诉我们那里是否有数据
int read = responseStream.EndRead(asyncResult);
if (read > 0)
{
//准备Char 数组缓冲区,用于向Unicode转换
Char[] charBuffer = new Char[BUFFER_SIZE];
//将字节流转换为Char 数组,然后转换为字符串
//len显示多少字符被转换为Unicode
int len = rs.StreamDecode.GetChars(rs.BufferRead, 0, read, charBuffer, 0);
String str = new String(charBuffer, 0, len);
rs.RequestData.Append(str);
IAsyncResult ar = responseStream.BeginRead(rs.BufferRead, 0, BUFFER_SIZE, new AsyncCallback(ReadCallBack), rs);
}
else
{
if (rs.RequestData.Length > 1)
{
// 所有数据都已被读取,因此将其显示到控制台
string strContent;
strContent = rs.RequestData.ToString();
Console.WriteLine(strContent);
}
//关闭响应流
responseStream.Close();
allDone.Set();
}
return;
}
}
2. 判断控件的Handle是在当前线程中,则无需异步执行,否则要异步
if (this.gcDC.InvokeRequired)
{
OnRegionChangedDelegate dlgt = new OnRegionChangedDelegate(OnLevelRegionChanged);
this.Invoke(dlgt, new object[] { sender, ev });
}
3. 开启新线程
可以通过开启新线程来执行异步操作,并给异步操作方法传递参数。参数通常包括异步方法所需的所有数据。
Thread thread=new Thread(new ParameterizedThreadStart(GetDataFromWebService));
private static void GetDataFromWebService(object entity){
showWindow();
getData();
window.clost();
}
4.在界面线程外打开程序
resultsCollection.AddRange(entity.DataCol);
InsertIntoTable(resultsCollection, "", entity.tableName, entity.Schema, "", entity.IsNewFetch);
if (window != null)
{
#region[Close progess window]
System.Windows.Application.Current.Dispatcher.Invoke(new Action(() =>
{
(window.WpfUIElement as System.Windows.Window).Close();
entity.SetNexValue();
}));
#endregion
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
中断线程。
追问
给个事例??
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询