[reflection] Coop handles icalls in System.Reflection and System.RuntimeTypeHandle...
[mono.git] / mcs / class / referencesource / System.Activities.Presentation / System.Activities.Presentation / System / Activities / Presentation / RetriableClipboard.cs
1 //----------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation.  All rights reserved.
3 //----------------------------------------------------------------
4
5 namespace System.Activities.Presentation
6 {
7     using System.Threading;
8     using System.Windows;
9     using System.Windows.Media.Imaging;
10     using System.Runtime.InteropServices;
11     using System.Runtime;
12     using System.Diagnostics;
13
14     // The clipboard may be accessed by other processes. 
15     // RetriableClipboard retries several times before giving up.
16     static class RetriableClipboard
17     {
18         const int retryCount = 10;
19         const int sleepTime = 50;
20
21         internal static IDataObject GetDataObject()
22         {
23             for (int i = 0; i < retryCount; i++)
24             {
25                 try
26                 {
27                     return Clipboard.GetDataObject();
28                 }
29                 catch (Exception err)
30                 {
31                     Trace.WriteLine(err.ToString());
32                     if (Fx.IsFatal(err))
33                     {
34                         throw;
35                     }
36                     Thread.Sleep(sleepTime);
37                 }
38             }
39             return null;
40         }
41
42         internal static void SetDataObject(object data, bool copy)
43         {
44             for (int i = 0; i < retryCount; i++)
45             {
46                 try
47                 {
48                     Clipboard.SetDataObject(data, copy);
49                     return;
50                 }
51                 catch (Exception err)
52                 {
53                     Trace.WriteLine(err.ToString());
54                     if (Fx.IsFatal(err))
55                     {
56                         throw;
57                     }
58                     Thread.Sleep(sleepTime);
59                 }
60             }
61         }
62
63         internal static void SetImage(BitmapSource image)
64         {
65             for (int i = 0; i < retryCount; i++)
66             {
67                 try
68                 {
69                     Clipboard.SetImage(image);
70                     return;
71                 }
72                 catch (Exception err)
73                 {
74                     Trace.WriteLine(err.ToString());
75                     if (Fx.IsFatal(err))
76                     {
77                         throw;
78                     }
79                     Thread.Sleep(sleepTime);
80                 }
81             }
82         }
83     }
84 }