开发者

C# Application is not intertactive while a loop runs in back

I have made an application in c# vs 2010 .net 4.0. It gets rss feed from gmail and then shows them in a simple table. But the problem start when i use a timer to check updates in string rss from a 40 sec time. The program works 开发者_如何学Cfine but it becomes unresponsive i cannot click a button or anything. I Need help. Note My App has to be operational after a task started.


Yes. Your application is doing its work on the UI thread without interacting with the Windows event loop. You need to do your work on a background thread (perhaps using BackgroundWorker).

Here is a very simple example using BackgroundWorker in a WinForms applcation (but do check the class documentation linked to above):

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        var bw = new BackgroundWorker();
        bw.DoWork += new DoWorkEventHandler(bw_DoWork);
        bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);
        bw.RunWorkerAsync();
    }

    // update UI back on main thread
    void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        listBox1.Items.AddRange(content.ToArray());
    }

    List<string> content;

    // do work on background thread
    void bw_DoWork(object sender, DoWorkEventArgs e)
    {
        content = new List<string>();
        // simulate slow update
        for (int i = 0; i < 1000; i++)
        {
            Thread.Sleep(10);
            content.Add(i.ToString());
        }
    }
}


I'd look into asynchronous methods to download like WebClient.DownloadStringAsync


Background worker or async web clients are the way to go. I would just like to add that I found strange behavior when I last used a webclient. Every first call I would make from a webclient object would result in about a 30-40 second wait, until the data was retrieved. Subsequent calls would be much faster. It ended up being something to do with the webclients default proxy setting, setting the proxy to null would eliminate the wait.

Setting the proxy to null would probably not be the best solution, just mentioning it in case it leads you to a better solution.


Do not use a background thread for this. Async != new thread. If you use a new thread to do synchronous web requests you'll (very quickly) starve the thread pool (Assuming you're using background workers).

A single thread can manage hundreds of asynchronous web requests but only one synchronous request at any one time.

If you're a beginner I highly recommend following CodeInChaos's advice and using a WebClient. The upside of a WebClient is it takes care of all the asynchronous plumbing for you. The downside is that because it does everything for you you lose a lot of control over the webrequest (I.e Can't modify http headers etc.) and it also returns the result on the UI thread (Which in your case is actually a positive thing).

var webUri = new Uri("www.website.com");
var webClient = new WebClient();

webClient.DownloadDataCompleted += DownloadDataCallback;
webClient.DownloadStringAsync(webUri, null);

private void DownloadDataCallback (Object sender, DownloadDataCompletedEventArgs e)
{
     var webRequestResult = e.Result;
}


Inside the loop, write the following line anywhere

Application.DoEvents()
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜