1

I am creating a time clock program. Our office staff would like to be able to send messages to specific employees when they clock in. The issue I have is if there are multiple messages to display, it loops through to the last one.

while(reader.Read()
{
     richtextbox.Text = reader["Message"].ToString();
     //need to pause here and wait for them to press the acknowledge button
{

Is there a way to wait or pause until the press the acknowledge button.

Any help is appreciated.

ps. I have tried using

public static class Utils
{
    public static Task WhenClicked(this Control target)
    {
        var tcs = new TaskCompletionSource<object>();
        EventHandler onClick = null;
        onClick = (sender, e) =>
        {
            target.Click -= onClick;
            tcs.TrySetResult(null);
        };
        target.Click += onClick;
        return tcs.Task;
    }
}

from this thread Wait until a click event has been fired C# but It did the same thing and blew past the button and showed the last message again.

3
  • what kind of program is this? what is its architecture? Commented Dec 14, 2018 at 18:02
  • Please show your attempted usage of WhenClicked. That solution should have worked Commented Dec 14, 2018 at 18:03
  • I am not exactly sure what you want me to show you. while(reader.Read() { richtextbox.Text = reader["Message"].ToString(); WhenClicked(button1); { this is what I put in the code. But like I said it skipped over all the other messages and just showed the last one. Is there anything specific you need me to show? I am very new at this and appreciate any help. The work around I have come up with for now is just to show a message box with the message being shown in the Message Box. It works but just annoying I cannot figure out how to do it the other way Commented Dec 14, 2018 at 18:59

1 Answer 1

1

I would approach the problem like this. Make your reader load all of the messages into a collection like a Queue (which is a first-in-first-out collection, ideal for what you are trying to do.) After the loop, if there are any messages, display the first one. Wire up the click event to a similar block of code that will display the next message (if any more exist) each time the button is clicked. The code would look like this:

Queue<string> EmployeeMessages = new Queue<string>();
private void OnLogin()
{
    var reader = GetReader();

    while (reader.Read())
    {
        EmployeeMessages.Enqueue(reader["Message"].ToString());
    }

    if (EmployeeMessages.Count > 0)
    {
        label1.Text = EmployeeMessages.Dequeue();
    }
}


private void button1_Click(object sender, EventArgs e)
{
    if (EmployeeMessages.Count > 0)
    {
        label1.Text = EmployeeMessages.Dequeue();
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

This was it! Works perfect! Not enough reputation for it to show as the marked answer but this is most definitely what I was looking for! Thanks for your Help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.