ASP.NET in C#: custom delegate function in QueueUserWorkItem not updating

245 views Asked by At

I have an aspx web page where a label should be updated with the message I pass into a delegate but every time I do this the label doesn't get updated. I'll give you some code below.

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 (code behind):

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);
    }
}

And finally the class where the event and delegate live:

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)

Now, everytime the method DoEncrypt gets added to QueueUserWorkItem it runs perfectly and everything gets done right. When debugging I noticed that the method feedbackInfo in my code behind gets called correctly to, here it is:

public void setFeedback(String message, bool info)
{
    if (info)
    {
        lblInfo.Text = message;
    }
    else
    {
        lblError.Text = message;
    }
}

I assume the method works because when I call the feedbackInfo method three times from within the ThreadPool, it does in fact get called three time. Each time the Text property of both labels contains the value it has been assigned to before.

So the only problem is, every time the label text property changes, the page isn't. Any ideas?

1

There are 1 answers

0
gmail user On

As you mentioned that asp:table is surrounded by update panel, so in your feedbackInfo or wherever you update the label, call the UpdatePanel.Update method.