开发者

Keeping memory in Tasks discrete

I've heard a LOT in the past about how programming with Threads and Tasks is very dangerous to the naive. Well, I'm naive, but I've got to learn sometime. I am making a program (really, it's a Generic Handler for ASP.Net) that needs to call to a 3rd party and wait for a response. While waiting, I'd like to have the handler continue doing some other things, so I am trying to figure out how to do the 3rd party web request asynchronously. Based on some answers to some other questions I've received, here is what I've come up with, but I want to make sure I won't get into big problems when my handler is called multiple time concurrently.

To test this I've built a console project.

class Program
{
    static void Main(string[] args)
    {
        RunRequestAsynch test = new RunRequestAsynch();
        test.TestingThreadSafety = Guid.NewGuid().ToString();

        Console.WriteLine("Started:" + test.TestingThreadSafety);

        Task tTest = new Task(test.RunWebRequest);
        tTest.Start();

        while (test.Done== false)
        {
            Console.WriteLine("Still waiting...");
            Thread.Sleep(100);
        }

        Console.WriteLine("Done. " + test.sResponse);

        Console.ReadKey();
    }
}

I instantiate a separate object (RunRequestAsynch) set some values on it, and then start it. While that is processing I'm just outputting a string to the console window.

public class RunRequestAsynch
{
    public bool Done = false;
    public string sResponse = "";
    public string sXMLToSend = "";

    public string TestingThreadSafety = "";

    public RunRequestAsynch() { }


    public void RunWebRequest()
    {
        Thread.Sleep(500);
        // HttpWebRequest stuff goes here
        sResponse = TestingThreadSafety;
        Done = true;
        Thread.Sleep(500);

    }
}

So...if I run 1000 of these simultaneously, I can count on the fact that each instance has its own memory and properties, right? And that the line "Done = true;" won't fire and then every one of the instances of the Generic Handler die, right?

I wrote a .bat file to run several instances, and the guid I set on each specific object seems to stay the same for each instance, which is what I wan开发者_Python百科t...but I want to make sure I'm not doing something really stupid that will bite me in the butt under full load.


I don't see any glaring problems, however you should consider using the Factory.StartNew instead of Start. Each task will only be executed once, so there isn't any problem with multiple tasks running simultaneously.

If you want to simplify your code a little and take advantage of the Factory.StartNew, in your handler you could do something like this (from what I remember of your last question):

Task<byte[]> task = Task.Factory.StartNew<byte[]>(() =>    // Begin task
{
    //Replace with your web request, I guessed that it's downloading data
    //change this to whatever makes sense
    using (var wc = new System.Net.WebClient())
        return wc.DownloadData("Some Address");
});

//call method to parse xml, will run in parallel

byte[] result = task.Result;  // Wait for task to finish and fetch result.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜