我有一个 aspx 网页,其中应该使用我传递给委托的消息来更新标签,但每次我这样做时,标签都不会更新。下面我给你一些代码。
默认.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;
}
}
我假设该方法有效,因为当我从 ThreadPool 中调用 feedbackInfo 方法三次时,实际上它确实被调用了三次。每次两个标签的 Text 属性都包含之前分配的值。
所以唯一的问题是,每次标签文本属性更改时,页面都不会更改。有任何想法吗?