How to ask current form to wait for user input

  • Thread starter Emma Middlebrook
  • Start date
E

Emma Middlebrook

Hi,

I have a form that executes a task in a separate thread, however
sometimes it's necessary to get some more information from the user
when the thread is executing. If this is the case, the form grows to
show a new section of the dialog where the edit controls are
displayed.

I've programmed the thread to call back into the form if this is the
case. My problem is that the form seems to hang when I try to sleep
the thread. I did have this working when it was a simple edit box and
button. When the user presses the button, the wait would end and the
other thread would then receive the input and continue executing. I've
now changed this to be a tab control because I needed to have other
controls displayed in this hidden area and as a result the form is now
hanging. Is there another way I could be doing this?

Here's a snippet from the executing thread when it calls back into the
parent form that launched the it.

try
{
string input =
m_parent.ObtainNewInput(item.DuplicatedItem;
SetNewAttributes(input);
}
catch
{
// The user cancelled the new input box.
uploaded = false;
break;
}

In the form, the function being called does the following:

public string ObtainNewInput(string original)
{
input.Text = original;
buttonClose.Enabled = false;
Size = new Size(528, 288);

tabPageCopy.Enabled = true;
tabExtras.SelectedItem = tabPageCopy;
tabPageCopy.Focus();
tabExtras.Refresh();
m_wait = true;
while (m_wait)
{
// Need to wait until the user has typed a new number
// and pressed ok.
Thread.Sleep(1000);
}
return input.Text;
}

when the user presses ok, the m_wait variable is set to false to stop
the loop and if the user presses cancel an exception is thrown to
abort the execution as it can't continue if this happens.

I think my problem is that I am sending the current thread to sleep
that is causing my hanging and I've no idea how it managed to work
previously. I can't use a new dialog, it has been requested that I
reuse this current dialog. But I need to pause the executing thread
until it receives this new information from the user and I'm not sure
how I can achieve this.

Regards,

Emma
 
J

Jon Skeet [C# MVP]

I think my problem is that I am sending the current thread to sleep
that is causing my hanging and I've no idea how it managed to work
previously. I can't use a new dialog, it has been requested that I
reuse this current dialog. But I need to pause the executing thread
until it receives this new information from the user and I'm not sure
how I can achieve this.

The easiest way would probably be to give the UI thread a monitor, and
use Wait/Pulse. So something like:

lock (myMonitor)
{
form.BeginInvoke (delegateToRunOnUIThread, ...);
Monitor.Wait(myMonitor);
}

Calling Monitor.Wait will give up the lock, so then on the UI thread,
you just need to do:

lock (theSameMonitor)
{
Monitor.Pulse(theSameMonitor);
}

when you've finished the work and made the information available (or if
the user chooses to cancel - you'll probably need to indicate that to
the worker thread with a separate flag, so it knows to quit instead of
continuing.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top