2

Okay so basically I have a function that returns a string, but to get that string it uses webrequest which means while it's doing that webrequest the form is locking up unless I put it in a different thread.

But I can't figure out a way to capture the returned data in a thread since it's started using thread.start and that's a void.

Any help please?

Current code if it matters to anyone:

string CreateReqThread(string UrlReq)
{
    System.Threading.Thread NewThread = new System.Threading.Thread(() => CreateReq(UrlReq));
    string ReturnedData = "";
    return ReturnedData;
}

string CreateReq(string url)
{
    try
    {
        WebRequest SendReq = WebRequest.Create(url);

        SendReq.Credentials = CredentialCache.DefaultCredentials;
        SendReq.Proxy = WebRequest.DefaultWebProxy;                 //For closed port networks like colleges
        SendReq.Proxy.Credentials = CredentialCache.DefaultCredentials;
        SendReq.Timeout = 15000;

        System.IO.StreamReader Reader = new System.IO.StreamReader(SendReq.GetResponse().GetResponseStream());
        string Response = Reader.ReadToEnd();
        Reader.Close();
        return Response;
    }
    catch (WebException e)
    {
        EBox(e.Message, "Unknown Error While Connecting");
        return null;
    }
}
1
  • 2
    This sounds like the perfect candidate for aync/await... have you considered reading about them? Commented Feb 13, 2013 at 0:06

1 Answer 1

3

A common means of doing this is to use a Task<T> instead of a thread:

Task<string> CreateReqThread(string UrlReq)
{
    return Task.Factory.StartNew() => CreateReq(UrlReq));

    // In .NET 4.5, you can use (or better yet, reimplement using await/async directly)
    // return Task.Run(() => CreateReq(UrlReq));
}

You can then call Task<T>.Result to get the returned value (later), when it's needed, or schedule a continuation on the task which will run when it completes.

This could look something like:

var request = CreateReqThread(theUri);
request.ContinueWith(t =>
{
     // Shove results in a text box
     this.textBox.Text = t.Result;
}, TaskScheduler.FromCurrentSynchronizationContext());

This also works perfectly with the new await/async support in C# 5.

Sign up to request clarification or add additional context in comments.

Comments

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.