开发者

WP7 HttpWebRequest check if file exists (synchronously)

开发者 https://www.devze.com 2023-02-17 14:11 出处:网络
I need to check if a file exists and I need to do it from several places in code. Some of the places I can handle it with a callback (kinda ugly but it will work).But the one I don\'t know how to hand

I need to check if a file exists and I need to do it from several places in code. Some of the places I can handle it with a callback (kinda ugly but it will work). But the one I don't know how to handle seems to require that it be Synchronous.

I need to call the method to check if it exist from a RelayCommand as the "canExecute" method. Any ideas on how to handle this?

This is what I currently have but calling the .WaitOne on the UI thread is blocking the background worker so it completely locks the app.

private bool FileExists(Uri file)  
    {  
        var exists = false;

        ManualResetEvent resetEvent = new ManualResetEvent(false);
        BackgroundWorker worker = new BackgroundWorker();
        worker.DoWork += (s, e) =>{

            WebRequest request = HttpWebRequest.Create(file);
            request.Method = "HEAD"; //only request the head so its quick

            request.BeginGetResponse(result =>
            {
                try
                {
                    //var response = request.EndGetResponse(result);
                    var req = (HttpWebRequest)result.AsyncState;
                    HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(result);

                    exists = (response.StatusCode.ToString() == "OK");
                }
                catch
                {
                    exists = false;

                }
                resetEvent.Set();
            }
            , reques开发者_Go百科t);
        };

        worker.RunWorkerAsync();
        resetEvent.WaitOne();

        return exists;
    }


You should never make HTTPWebRequest's synchronous on the UI thread - this could block the UI for seconds or minutes...

If you really want to make an HTTPWebRequest appear to be synchronous on a background thread then simply use a ManualResetEvent inside a callback - e.g. something like:

var resetEvent = new ManualResetEvent();
theHttpWebRequest.BeginGetResponse((result) => {
       var response = theHttpWebRequest.EndGetResponse(result);
       // use response.StatusCode to check for 404?
       resetEvent.Set();
    });
resetEvent.WaitOne();

Also, please note that checking if a file exists over HTTP might be better done by calling a small webservice which does the check - it depends on the size of the file you are checking.


AFAIK this is not possible. You can never make synchronous calls to web services in Silverlight.

You have to leave canExecute method empty (to always execute the command), and make async call to check if file exists in handler for the command. The real code for the command has to execute in handler for that async call.

I think it is only way you can manage it.

btw-you can use lambda expressions to make it look more like synchronous code. Or maybe Reactive Extensions may help with better looking code (jesse's tutorial).


The way I would approach this problem is to create some kind of flag ( i.e IsFileExists) and return that flag from CanExecute method. Flag shold be set to false initially and your button disabled under assumption that untill we know that file does exits we consider it doesn't. Next I would fire HTTPWebRequest or wcf call or any other async method to check if file exists. Once callback confirms that file exists set flag to true and fire CanExecuteChanged event. If you want to be fancy you can add some visual feedback while waiting for responce. In general user experienc would be much better than locking up screen for duration of the web request.

0

精彩评论

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