C#委托或线程 [英] C# Delegate OR threading
问题描述
在我的asp.net Web应用程序中,我执行了冗长的执行过程,例如单词合并.我想合并Word文档并在过程中发送给客户端.基本上,我正在寻找运行,而忘记了一种异步过程.
In my asp.net web application i have lengthy excution process like word merge. I want to merge the word documents and send to client in a procedure. Basically i am looking for run and forget kind of asynchronous process.
在我的for循环中,我尝试创建委托和rstrat.像这样
In my for loop i tried to create delegate and rstrat. like this
filenames是一个字符串数组,在调用委托之前,在相同的循环中创建并辅助值.
filenames is a string array, creating in the same loop and assiging values before calling delegate.
MailSender 邮件= 新 MailSender ();
MailSender mail = new MailSender();
mail.FILESTOMERGE =文件名;
mail.FILESTOMERGE = filenames;
mail.Secondparameter = param;
mail.Secondparameter=param;
.. setting许多这样的属性.然后我创建代理,其中邮件是类的对象 MailSender 并且具有合并方法
..setting lots of properties like this. Then i create the delegate where mail is object of class MailSender and having the method Merge
TestDelegate testdele = 新 TestDelegate (mail.Merge);
TestDelegate testdele = new TestDelegate(mail.Merge);
IAsyncResult iasy = testdele.BeginInvoke( , 空 );
IAsyncResult iasy = testdele.BeginInvoke(null, null);
然后递增i值并循环播放.
因此将使用不同的文件名多次调用delegae函数.但是我收到错误消息,说文件名数组中没有值.当我设定一个断点并观察发生了什么事时,我发现有 交叉引用(如文件名aray)包含第一个委托对象提供的值,而param值包含该foor循环的最后一个值.
So there will be mutiple calls to delegae function with different filenames. But i am getting error saying that there is no values in filenames array. When i put a break point and watch what's happening, i saw that there are cross references like filenames aray contains the values which is suplied with first delegate obje and param value contains last value of that foor loop.
Makea指出,每次在for循环中我都在创建MailSender()对象.为什么不创建不同的onject并使用每次设置的属性调用Merge方法.
使用Thread可以解决此问题吗?
任何帮助表示赞赏....
推荐答案
没有足够的信息来回答您的问题.
Not enough info to answer your question.
谢谢,鲁迪.
这篇关于C#委托或线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!