I need to call class method in separate thread. The method has a parameter and a return value.
Details
I have a Form with two TextBoxes. User put value in the first TextBox and get the result in the second one:
private void tbWord_TextChanged(object sender, TextChangedEventArgs e)
{
tbResponce.Text = Wiki.DoWork(tbWord.Text);
}
The Wiki-class must use Wikipedia API:
public class Wiki
{
private class State
{
public EventWaitHandle eventWaitHandle = new ManualResetEvent(false);
public String result;
public String word;
}
private static void PerformUserWorkItem( Object stateObject )
{
State state = stateObject as State;
if(state != null)
{
Uri address = new Uri("http://en.wikipedia.org/w/api.php?action=opensearch&search=" + state.word);
HttpWebRequest request = WebRequest.Create(address) as HttpWebRequest;
HttpWebResponse responce = (HttpWebResponse)request.GetResponse();
StreamReader myStreamReader = new StreamReader(responce.GetResponseStream(), Encoding.GetEncoding(1251));
state.result = myStreamReader.ReadToEnd();
state.eventWaitHandle.Set();
}
}
public static String DoWork(String _word)
{
State state = new State();
state.word = _word;
ThreadPool.QueueUserWorkItem(PerformUserWorkItem, state);
state.eventWaitHandle.WaitOne();
return state.result;
}
}
Problem
When user press keys in the tbWord the main form freeze and wait while Wiki class do all the work.
How can I run DoWork asynchronously?
Or use TPL like this:
see: http://msdn.microsoft.com/en-us/library/dd997394.aspx