|
That's a database question.
Did you specify that the password should be saved to the UDL file?
Did you look in the UDL file and confirm that it's there?
|
|
|
|
|
Dear Coders.
I have a form, with a text box to allow a user to type a question, and a scrollable edit box that instantly relays the question.
Then, I send the typed question to my object, and it produces a responce (a bit like the AI webbots on here)
I want a delay while waiting for the responce, so it is more realistic.
I am using Thread.Sleep(3000) to produce a 3 second pause.
Now, this works fine EXCEPT, while the program is pausing, the text that the user has entered does not appear in the chat box.
once the pause has finished, the text AND the responce end up in the chat box at the same time.
So, when a user types a question, I would like it to appear straight away in the chat box, and only the reponse should be delayed.
I have tried moving the sleep function into every different call I make to see if it that but it makes no difference.
I even tried to call invalidate() on my edit box to update the display BEFORE I call sleep(), but again, no help.
Can anyone shed some light on this please?
Thank you
Steve
|
|
|
|
|
You'll have to use a thread to do the waiting bit, because when your're using Thread.Sleep in an event handler
the GUI thread can't be used to do all the repainting stuff that is needed.
My advice is:
Start a thread that waits (WaitOne) on a WaitHandle (e.g. AutoResetEvent) to process a
Queue<string> obj (queue of strings). When a string is entered by the user it's put on the
queue and the WaitHandle is signaled. The thread will awaken take the first string on the queue
(after locking on it of course). After the thread released the lock on the queue it'll go to
sleep and after awaking from sleep write response into textbox (remember to use invoke when doing
stuff on the GUI from a thread different from the GUI thread). After writing to the textbox the
thread will do WaitOne.
Hope this helps!
|
|
|
|
|
ManfredRBihy wrote: Hope this helps! Smile
Nope, sorry, that went straight over my head.
I dont even know where to start with this, as I am not used to working with threads.
Is there not just a simple way for me to say Sleep() just before I call for the responce, so that the first string (from the user) goes straight to the chat box, then sleep is called, waits, then ouputs the responce?
If I try to do what you have told me I realy dont know where to start.
Thank you for your help though.
Steve
|
|
|
|
|
I'll try to explain it in more detail.
A forms application has a thread to handle all the events
that are happening when the user enters input or clicks
something with the mouse. When an event handler is attached
to a forms object and it does a Thread.Sleep(xxxx) events
can no longer be handled and the application seems to freeze
for time specified in the Thread.Sleep call.
One thing you might try if you're not yet comfortable with creating
your own Thread is using a timer on your form. When the text is entered
the timer is set and activated. In the event handler of the timer the text
will finally be written to the textbox. Since all this is happending on
the GUI thread you don't even have to worry about that invoke bit I mentioned
in my previous response.
Better?
|
|
|
|
|
ManfredRBihy wrote: Better? Wink
Yes, I am "slightly" more comfotable with timers, so I will give that ago for now.
I am awaighting my new book on c# from amazon so I can get into the learning process better, and hopefully reduce the need to ask so many questions on here.
thank you again
|
|
|
|
|
Aww it didnt work.
It paused but the same thing happned
Steve
|
|
|
|
|
Can you post some code?
That would really be helpful in helping you !
(Remember to use "code block" when posting the code bits.)
|
|
|
|
|
OK, here you go, but please bare in mind I am a learner.
Also, It sort of works now, but the text that is output last is added onto the new text, so I am going wrong somewhere.
This part is elisa (name of the AI reponder)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace AI
{
class elisa
{
private string input;
private string output;
private int NumberOfReplies = 0;
public string respond(string input)
{
output = ("");
this.input = input;
if (NumberOfReplies >= 1)
{
output = ("Please wait!");
}
else
output = ("Sorry am a bit buzy at the min. ");
NumberOfReplies += 1;
Thread.Sleep(4000);
return output;
}
}
}
And this is from my form, it has a text box to enter what you want to say, and an edit box that then shows that text, and shows the responce from elisa...
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace AI
{
public partial class Form1 : Form
{
elisa _elisa = new elisa();
string userInput;
string elisaResponce;
string you = ("You: ");
string elisa = ("Elisa: ");
public Form1()
{
InitializeComponent();
status.Text = ("");
}
private void btnSend_Click(object sender, EventArgs e)
{
updateInput(textBoxInput.Text);
textBoxInput.Clear();
status.Text = ("Elisa is typing a message");
getResponceFromElisa();
}
private void updateInput(string input)
{
richTextBoxOutput.Text += you += input + ("\n");
}
private void getResponceFromElisa()
{
timer1.Start();
}
private void textBoxInput_TextChanged(object sender, EventArgs e)
{
if (textBoxInput.Text == (""))
{
status.Text = ("");
}
else
status.Text = ("You are typing a message...");
}
private void timer1_Tick(object sender, EventArgs e)
{
elisaResponce = _elisa.respond(userInput);
richTextBoxOutput.Text += elisa += elisaResponce;
timer1.Stop();
status.Text = ("");
}
}
}
Thank you for asking to have a look, I appreciate it
Steve
|
|
|
|
|
I'll have a look at your post when I'm home from work.
(And I'm getting ready to go just about now)
Bye for now
|
|
|
|
|
ManfredRBihy wrote: I'll have a look at your post when I'm home from work.
(And I'm getting ready to go just about now)
Thank you
Steve
|
|
|
|
|
your problem lies in elisa.respond() . Inside it lies the folowing code: Thread.Sleep(4000); . You use that method in a Timer.Tick event. So even if timer ticks, the event doesn't finish for next event tick
|
|
|
|
|
this is a quick hack:
try a whateverControl.Refresh() before you put your main thread to sleep.
A thread or timer approach is better though as it would keep your app responsive.
|
|
|
|
|
OK, thank you.
Can you see anywhere in my code why it is that the strings are being added together?
For example, if I type:
Hello.
Then I type hello again, it would output as
hellohello.
I have tried to clear the strings atraight after use but no luck.
Thank you
Steve
|
|
|
|
|
stephen.darling wrote: I have tried to clear the strings atraight after use but no luck
stephen.darling wrote: richTextBoxOutput.Text += you += input + ("\n")
two very strange statements.
|
|
|
|
|
Luc Pattyn wrote: two very strange statements.
Maybe so, but this is me we are talking about, you proberbly know by bow how difficult it is to explain things to you.
You would think, as a Biomedical Scientist that I would not have a issue, but when it comes to a new area, for me, it is
I am awaiting delivery of my new book, head first c#, so hopefully I can start learning properly.
Regards,
Steve
|
|
|
|
|
Here's an extremely cheesy hack. It's only merit (if you can call it that) is that it's quick to write. For grins, you can show/hide a marquee style progress bar before/after calling GenerateResponse() to simulate processing.
string GenerateResponse
(string input)
{
DateTime start = DateTime.Now;
int pause = 3000;
while ((DateTime.Now - start).TotalMilliseconds < pause) {
Application.DoEvents();
}
return "Some response based on " + input;
}
/ravi
|
|
|
|
|
You're a dirty, dirty bird!
|
|
|
|
|
Hence the disclaimers.
If all he wants to do is write a fun little app, it'll do. But I would never put anything like this in my freeware or commercial apps.
/ravi
|
|
|
|
|
How about this?
string GenerateResponse
(string input)
{
DateTime start = DateTime.Now;
int pause = 3000;
loop:
if ((DateTime.Now - start).TotalMilliseconds >= pause) {
return "Some response based on " + input;
}
Application.DoEvents();
goto loop;
}
/ravi
|
|
|
|
|
Now that's just sad, so, so sad
|
|
|
|
|
Go on - 5 it. You know you wanna.
/ravi
|
|
|
|
|
If by "5 it", you mean "punch it in the face with my fist", then I'm sure he does want to.
|
|
|
|
|
Having trouble seeing the smilies?
/ravi
|
|
|
|
|
I too was joking (making reference to hands having 5 fingers). Didn't feel the need to taint the joke with an emotion though.
|
|
|
|