如何将变量与PageAsyncTask关联 [英] How to associate a variable with PageAsyncTask

查看:60
本文介绍了如何将变量与PageAsyncTask关联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在尝试异步处理文件,如下所示:

Hi,

I am trying to process files asynchronously as shown below:

 for (int i = 0; i < FileCount; i++)
 {
                
 FilePath = ViewState["SubFileName"].ToString() + (i + 1).ToString()  + ".sdf";
                               
        GetTask();
               
 }

void GetTask()
    {

        PageAsyncTask Task = new PageAsyncTask(new  BeginEventHandler(this.OnBeginFunct), new EndEventHandler(this.OnEndFunct), new EndEventHandler(this.TimeoutHandler), true, true);
            Page.RegisterAsyncTask(Task);
    }

    IAsyncResult OnBeginFunct(object sender, EventArgs e, AsyncCallback cb, object state)
    {
  AsyncTaskDelegate _runnerDelegateChem = null;
         _runnerDelegateChem = new AsyncTaskDelegate(FuncProcessFile);
               return result; 
    }

    void OnEndFunct(IAsyncResult ar)
    {
        
 //code to release all object and temp files
    }

Void FuncProcessFile()
{
 //code to process file
}




在上面的代码中,为"n"个文件创建了"n"个任务(请参见循环).但是在FuncProcessFile函数内部,总是处理最后一个文件.请让我知道如何将FilePath变量与每个任务相关联.

谢谢




In the above code ''n'' number of task are created for ''n'' number of files(see for loop). But inside FuncProcessFile function always last file is processed. Please let me know how to associate FilePath varible with each task.

Thanks

推荐答案

你好Thakur,

您尝试过以下吗?

修改您的委托并添加参数:
Hello Thakur,

Have you tried the following?

Modify your delegate and add parameter:
protected delegate void AsyncTaskDelegate(string sampleParam);



在您的OnBeginFunct中,替换为以下内容:
我不知道您在旧代码中从何处获得结果值,但是我所做的是添加以下 IAsyncResult result = _runnerDelegateChem.BeginInvoke(sampleParam,cb,null); 这是丢失的您的代码中的项目.



In your OnBeginFunct replace with the following:
I don''t know where did you get the result value in your old code but what i did is add the following IAsyncResult result = _runnerDelegateChem.BeginInvoke(sampleParam, cb, null); which is the missing item in your code.

IAsyncResult OnBeginFunct(object sender, EventArgs e, AsyncCallback cb, object state)
{
	//Old
	//AsyncTaskDelegate _runnerDelegateChem = null;
	//_runnerDelegateChem = new AsyncTaskDelegate(FuncProcessFile);
	//return null;

	//New
	string sampleParam = "something path file here"; //use function to get current path file.
	AsyncTaskDelegate _runnerDelegateChem = new AsyncTaskDelegate(this.FuncProcessFile);
	IAsyncResult result = _runnerDelegateChem.BeginInvoke(sampleParam, cb, null);
	return result;
}



最后在FuncProcessFile中添加一个参数,并确保它与您的委托匹配. :)



and lastly add one parameter in your FuncProcessFile and make sure that it matches to your delegate. :)

void FuncProcessFile(string sampleParam)



仅供参考,我没有验证此代码,因此可能存在拼写错误. :)



FYI I did not verify this code so there''s probably typo error. :)


这篇关于如何将变量与PageAsyncTask关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆