프로젝트

일반

사용자정보

개정판 6a19b48d

ID6a19b48d4d65f0132542748dacd26fb0b1c9c89a
상위 c1946e9c
하위 d2050059, f5f788c2

김태성이(가) 약 2년 전에 추가함

issue #00000 페이지 이미지 download 로직 수정

Change-Id: I4da03c36b5a135eea80b57b20ada38239afd1082

차이점 보기:

KCOM/PageManager/PageStorage.cs
26 26
        int _TotalPages;
27 27
        int _TakeCount;
28 28

  
29
        bool IsDownload = false;
29
        bool IsBusy = false;
30 30

  
31 31
        BitmapFrame PageImage;
32 32

  
......
37 37
                backgroundWorker = new BackgroundWorker { WorkerSupportsCancellation = true, WorkerReportsProgress = true };
38 38
                backgroundWorker.DoWork += BackgroundWorker_DoWork;
39 39
                backgroundWorker.ProgressChanged += BackgroundWorker_ProgressChanged;
40
                
40 41
                _fileExt = fileExt;
41 42
                _BaseUri = BaseUri;
42 43
                LocalStorage = localStoragePath;
......
63 64

  
64 65
            while (WorkItems.Count > 0)
65 66
            {
66
                if (!IsDownload)
67
                if (!IsBusy)
67 68
                {
68 69
                    int item = -1;
69 70
                    try
......
106 107
            try
107 108
            {
108 109

  
109
                var localUri = await GetPageUriAsync(null, PageNo);
110
                var localUri = await GetPageUriAsync(cts, PageNo);
110 111

  
111
                if (localUri != null || !cts.IsCancellationRequested)
112
                if (localUri != null) // || !cts.IsCancellationRequested
112 113
                {
113 114
                    PageImage = BitmapFrame.Create(localUri, BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
114 115
                }
......
190 191

  
191 192
                var pageItem = await DownloadPageAsync(PageNo, cts);
192 193

  
193
                if (pageItem != null && 0 < _TakeCount && PageNo + 1 < _TotalPages)
194
                if (pageItem != null)
194 195
                {
195
                    int takecount = _TakeCount;
196
                    result = pageItem.LocalUri;
196 197

  
197
                    if (_TotalPages < PageNo + 1 + takecount)
198
                    if(PageNo + _TakeCount < _TotalPages)
198 199
                    {
199
                        takecount = takecount - (PageNo + 1 + takecount - _TotalPages) - 1;
200
                        int takecount = 5;
201

  
202
                        if (_TotalPages < PageNo + takecount)
203
                        {
204
                            takecount = takecount - (PageNo + takecount - _TotalPages) - 1;
205
                        }
206

  
207
                        DownloadWorkAsync(PageNo + 1, takecount);
200 208
                    }
209
                    //int takecount = _TakeCount;
210

  
211
                    //if (_TotalPages < PageNo + takecount)
212
                    //{
213
                    //    takecount = takecount - (PageNo + takecount - _TotalPages) - 1;
214
                    //}
215

  
216
                    //DownloadWorkAsync(PageNo + 1, takecount);
217

  
201 218

  
202
                    DownloadWorkAsync(PageNo + 1, takecount);
203 219
                    //var takePageNoList = Enumerable.Range(PageNo + 1, _TakeCount);
204 220
                }
221
                else
222
                {
205 223

  
206
                result = pageItem.LocalUri;
224
                }
207 225
            }
208 226
            catch (Exception ex)
209 227
            {
210
                if (cts != null)
211
                {
212
                    if (cts.Value.IsCancellationRequested)
213
                    {
214
                        return result;
215
                    }
216
                }
228
                //if (cts != null)
229
                //{
230
                //    if (cts.Value.IsCancellationRequested)
231
                //    {
232
                //        return result;
233
                //    }
234
                //}
217 235

  
218
                if (!IsRestart)
219
                {
220
                    await Task.Delay(100);
236
                //if (!IsRestart)
237
                //{
238
                //    await Task.Delay(100);
221 239

  
222
                    result = await GetPageUriAsync(cts, PageNo, true);
223
                }
240
                //    result = await GetPageUriAsync(cts, PageNo, true);
241
                //}
224 242
                //throw new Exception("GetPageAsync(string BasePageUri,int PageNo)", ex);
225 243
            }
226 244
            finally
......
254 272
                }
255 273
                else
256 274
                {
275
                    
257 276

  
258 277
                    if (!System.IO.Directory.Exists(LocalStorage))
259 278
                    {
260 279
                        System.IO.Directory.CreateDirectory(LocalStorage);
261
                        System.Threading.Thread.Sleep(100);
280
                        System.Threading.Thread.Sleep(50);
262 281
                    }
263 282

  
264 283
                    System.Diagnostics.Debug.WriteLine("DownloadPageAsync down");
......
275 294
                        LocalFilePath = downloadFilePath
276 295
                    };
277 296

  
278
                    if (fileItems.Where(x => x.PageNo == PageNo).Count() == 0)
279
                    {
280
                        fileItems.Add(result);
281
                    }
282

  
283 297
                    using (System.Net.WebClient client = new System.Net.WebClient())
284 298
                    {
299
                        System.Diagnostics.Debug.WriteLine("download start " + downloadFilePath);
285 300
                        client.CachePolicy = new System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.NoCacheNoStore);
286 301
                        client.Headers.Add("Cache-Control", "no-cache");
287 302
                        client.UseDefaultCredentials = true;
......
302 317

  
303 318
                        if (cts != null)
304 319
                        {
305
                            client.DownloadProgressChanged += (snd, ect) =>
320
                            cts.Value.Register(() =>
306 321
                            {
307
                                IsDownload = client.IsBusy;
322
                                client.CancelAsync();
323
                                Console.WriteLine("Request cancelled!");
324
                                result = null;
325
                            });
308 326

  
309
                                if (cts.Value.IsCancellationRequested)
310
                                {
311
                                    client.CancelAsync();
312
                                }
327
                            client.DownloadProgressChanged += (snd, ect) =>
328
                            {
329
                                IsBusy = client.IsBusy;
330

  
331
                                //if(cts.Value.IsCancellationRequested)
332
                                //{
333
                                //    System.Diagnostics.Debug.WriteLine("download cancel " + downloadFilePath);
334
                                //    client.CancelAsync();
335
                                //    client.Dispose();
336
                                //}
313 337
                            };
314 338
                        }
315 339

  
316 340
                        client.DownloadFileCompleted += (snd, evt) =>
317 341
                        {
318
                            result.IsDownLoad = true;
319
                            System.Diagnostics.Debug.WriteLine("Download : " + downloadFilePath);
342
                            if (!evt.Cancelled)
343
                            {
344
                                result.IsDownLoad = true;
320 345

  
321
                            PageLoadCompleted?.Invoke(this, new PageLoadCompletedEventArgs(result));
346
                                if (fileItems.Where(x => x.PageNo == PageNo).Count() == 0)
347
                                {
348
                                    fileItems.Add(result);
349
                                }
350

  
351
                                System.Diagnostics.Debug.WriteLine("Download completed finish : " + downloadFilePath);
352

  
353
                                PageLoadCompleted?.Invoke(this, new PageLoadCompletedEventArgs(result));
354

  
355
                                client.Dispose();
356
                            }
357
                            else
358
                            {
359
                                System.Diagnostics.Debug.WriteLine("Download completed cancelled : " + downloadFilePath);
360

  
361
                                client.Dispose();
362
                                System.IO.File.Delete(downloadFilePath);
363
                            }
322 364
                        };
323 365

  
324 366
                        await client.DownloadFileTaskAsync(originalUri, downloadFilePath);
......
335 377
            }
336 378
            finally
337 379
            {
338
                IsDownload = false;
380
                IsBusy = false;
339 381
            }
340 382

  
341 383
            return result;

내보내기 Unified diff

클립보드 이미지 추가 (최대 크기: 500 MB)