C#中的ASP.NET:QueueUserWorkItem中的自定义委托功能没有更新
问题描述:
我有一个aspx网页,其中的标签应该更新为我传入委托的消息,但每次我都这样做时,标签不会更新。我会在下面给你一些代码。C#中的ASP.NET:QueueUserWorkItem中的自定义委托功能没有更新
的Default.aspx
<!-- This table has a surrounding updatepanel -->
<asp:TableRow>
<asp:TableCell><asp:Button id="encrypteer" Text="Encrypteer" runat="server" OnClick="encrypteer_Click" ToolTip="Encrypteer uw bestand" /></asp:TableCell>
<asp:TableCell><asp:Button id="decrypteer" runat="server" Text="Decrypteer" OnClick="decrypteer_Click" ToolTip="Decrypteer uw bestand" /></asp:TableCell>
</asp:TableRow>
<asp:TableRow ForeColor="Blue">
<asp:TableCell ID="infoCell" runat="server" ColumnSpan="2">
Info: <asp:Label ID="lblInfo" runat="server" Text="Cryptografie nog niet gestart." />
</asp:TableCell>
</asp:TableRow>
<asp:TableRow ForeColor="Red">
<asp:TableCell ID="errorCell" runat="server" ColumnSpan="2">
Fouten: <asp:Label ID="lblError" runat="server" Text="Geen fouten." />
</asp:TableCell>
</asp:TableRow>
default.aspx.cs(代码后面):
protected void encrypteer_Click(object sender, EventArgs e)
{
if (getKeys())
{ // Ingevoerde sleutels ophalen
if (taal.SelectedIndex == 0) //crypto.DoCryptFile(EncryptNoLibraries.CryptType.ENCRYPT, sleutels); // en deze meegeven in de encryptieklasse
{
crypto = new EncryptNoLibraries(@"C:\Users\Robbie\TestDES\test.txt", @"C:\Users\Robbie\TestDES\des.txt");
crypto.feedbackInfo += new EncryptNoLibraries.Feedback(setFeedback);
object[] allArgs = { EncryptNoLibraries.CryptType.ENCRYPT, lstSleutels };
object args = allArgs;
ThreadPool.QueueUserWorkItem(new WaitCallback(crypto.DoCryptFile), args);
}
else if (taal.SelectedIndex == 1)
EncryptLibraries.EncryptFile(@"C:\Users\Robbie\TestDES\test.txt", @"C:\Users\Robbie\TestDES\des.txt", txtSleutel.Text);
}
}
最后类该事件并委托现场:
public class EncryptNoLibraries
{
public event Feedback feedbackInfo;
public EventArgs e = null;
public delegate void Feedback(String message, bool info);
public enum CryptType : int { ENCRYPT, DECRYPT };
private CryptType cryptType;
//More irrelevant declarations
public EncryptNoLibraries()
{ }
public EncryptNoLibraries(String strFilePath, String strPathToSave)
{
this.strPathForSourceFile = strFilePath;
this.strPathToDestinationFile = strPathToSave;
}
public void DoCryptFile(object args)
{
//CryptType type, List<byte[]> desSleutels
object[] allArgs = (object[])args;
cryptType = (CryptType) allArgs[0];
sleutels = (List<byte[]>) allArgs[1];
if (cryptType == CryptType.ENCRYPT)
strType = "encrypteren";
else
strType = "decrypteren";
if (strPathForSourceFile.Equals(String.Empty)) // Als geen bestand gekozen werd...
feedbackInfo(String.Format("Kies een bestand voor u gaat {0}.", strType), false); // geven we een gepaste tekst.
else
{
List<BitArray> lstSplittedFile = splitFileIntoBlocksOf64Bits(); //Gesplitst bestand ophalen (Lijst van BitArrays[64])
//feistel.setKey(new BitArray(sleutelEen)); //De 16 subsleutels laten genereren
feistel.setKey(Arrays.ReverseBitArray(new BitArray(sleutels[0])));
lstCodedFile = new ArrayList(lstSplittedFile.Count); //Fills the list with empty byte arrays
feedbackInfo(String.Format("Bezig met {0}: DES", strType), true);
//This function goes on a while with a few occurrences of feedBackInfo(String, bool)
现在,每次将方法DoEncrypt添加到QueueUserWorkItem中时,它都可以完美运行,并且一切都可以正确完成。 调试时我发现,在我的代码的方法feedbackInfo背后被正确地叫,那就是:
public void setFeedback(String message, bool info)
{
if (info)
{
lblInfo.Text = message;
}
else
{
lblError.Text = message;
}
}
我假设的方法工作,因为当我打电话的feedbackInfo方法三次从线程池内,它实际上被称为三次。每次标签的Text属性都包含之前分配的值。
所以唯一的问题是,每次标签文本属性发生变化时,页面都不是。有任何想法吗?
答
至于你提到asp:table
被update panel
包围,所以在您的feedbackInfo
或任何你更新label
,调用UpdatePanel.Update
方法。
你解决了这个问题吗?如果答案是肯定的,那么其他人可以使用它。 –