0

複数のソースからのソースデータを持つWCFサービスを作成しています。これらはさまざまな形式の大きなファイルです。

キャッシュを実装し、ポーリング間隔を設定して、これらのファイルが最新のデータで最新の状態に保たれるようにしました。

基本的にXDocumentオブジェクトを呼び出し元に返す役割を担うマネージャークラスを作成しました。マネージャークラスは、最初にキャッシュの存在を確認します。存在しない場合は、新しいデータを取得するための呼び出しを行います。ここには大きなものはありません。

応答をスナッピーに保つために私がしたいのは、以前にダウンロードしたファイルをシリアル化し、それを呼び出し元に返すことです-これも新しいことではありません...しかし...シリアル化が完了したらすぐに新しいスレッドを生成したい新しいデータを取得し、古いファイルを上書きします。これが私の問題です...

確かに中級のプログラマーです-私はマルチスレッドに関するいくつかの例に出くわしました(ここではそのことについて)...問題はそれがデリゲートの概念を導入したことであり、私はこれに本当に苦労しています。

これが私のコードの一部です:

//this method invokes another object that is responsible for making the 
    //http call, decompressing the file and persisting to the hard drive.
    private static void downloadFile(string url, string LocationToSave)
    {
        using (WeatherFactory wf = new WeatherFactory())
        {
            wf.getWeatherDataSource(url, LocationToSave);
        }
    }

    //A new thread variable
    private static Thread backgroundDownload;

    //the delegate...but I am so confused on how to use this...
    delegate void FileDownloader(string url, string LocationToSave);

    //The method that should be called in the new thread....
    //right now the compiler is complaining that I don't have the arguments from
    //the delegate (Url and LocationToSave...
    //the problem is I don't pass URL and LocationToSave here...
    static void Init(FileDownloader download)
    {
        backgroundDownload = new Thread(new ThreadStart(download));
        backgroundDownload.Start();
    }

これを正しい方法で実装したいので、この作業を行う方法について少し教育していただければ幸いです。

4

1 に答える 1

1

これを行うには、 Task Parallel ライブラリを使用します。

//this method invokes another object that is responsible for making the 
//http call, decompressing the file and persisting to the hard drive.
private static void downloadFile(string url, string LocationToSave)
{
    using (WeatherFactory wf = new WeatherFactory())
    {
        wf.getWeatherDataSource(url, LocationToSave);
    }
    //Update cache here?
}

private void StartBackgroundDownload()
{
    //Things to consider:
    // 1. what if we are already downloading, start new anyway?
    // 2. when/how to update your cache
    var task = Task.Factory.StartNew(_=>downloadFile(url, LocationToSave));
}
于 2013-03-16T18:59:27.393 に答える