Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7546669
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T09:15:32+00:00 2026-05-30T09:15:32+00:00

I need to call class method in separate thread. The method has a parameter

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T09:15:34+00:00Added an answer on May 30, 2026 at 9:15 am

    Or use TPL like this:

    private void tbWord_TextChanged(object sender, TextChangedEventArgs e) 
     { 
        Task.Factory.StartNew(() =>
        {
            return Wiki.DoWrok(tbWord.Text);
        }).ContinueWith(taskState =>
        {
            tbResponce.Text = taskState.Result;
        }, TaskScheduler.FromCurrentSynchronizationContext());
     } 
    

    see: http://msdn.microsoft.com/en-us/library/dd997394.aspx

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class that has two method in it, one calls a class
I have a vector of pointers to a class. I need to call their
I have small class called 'Call' and I need to store these calls into
I currently have a need for a custom ListViewItem class - let's call it
I need to interpose on a method call in a C++ program (the class
I have a Windows Form and a class with two simple methods that run
I have a boost based thread(let's call it T-thread) separate from the iphone's main
I have an object pool, and I need to call a delegate method OnFree(),
I need to call the co-class function by reading its address from vtable of
I need to call a method that accepts a stream argument. The method loads

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.