AiNoTame

...for love
posts - 9, comments - 40, trackbacks - 0

WPF DataGrid Clipboard BUG(?)&Workaround

 

Il mese scorso mi sono imbattuto in questo strano problema: La mia applicazione crashava su un particolare computer (che, per curiosità, aveva WinXP SP3 ma non credo sia questo il problema). L’utente sostiene che l’applicazione crasha anche stando a guardarla… molto strano.

0) Scoperta

L’eccezione non gestita, ricavata sia dal log applicativo (catturata nell’evento Application.DispatcherUnhandledException) sia da Event Viewer, era la seguente:

OpenClipboard Failed (Exception from HRESULT: 0x800401D0 (CLIPBRD_E_CANT_OPEN))

Dallo stack trace si evinceva che l’eccezione veniva lanciata dalla DataGrid di WPF4. Bene… andiamo in dettaglio.

1) Teoria della clipboard (fonte: Clipboard on MSDN Library)

Come è ovvio, la clipboard è un elemento di sistema a cui possono accedere più applicazioni contemporaneamente. A livello di API, ogni applicazione esegue, in genere, i seguenti passaggi:
- OpenClipboard()
- clear/read/set data
- CloseClipboard()

OpenClipboard() ritorna true se l’applicativo riesce a avere possesso della clipboard. In questo momento solamente lui è in grado di accedervi. Nessun’altra applicazione può accedervi (sia in scrittura che in lettura) prima che essa venga rilasciata con CloseClipboard().

2) Riproduzione del problema:

Create due applicazioni. La prima, un semplice Console Application, che esegue il seguente codice:

[DllImport("user32.dll")]
extern static bool OpenClipboard(IntPtr hWnd);
if (OpenClipboard(IntPtr.Zero))//lock clipboard 
    Thread.Sleep(60000);//così ho tempo per lanciare l’altra applicazione.

La seconda, WPF application, con un textbox e una DataGrid popolata e con elementi ClipboardContentBinding. Ora lanciate (non da VS) la console application, debuggate l’app WPF.
Premete CTRL+C sulla textbox. Blocco di pochi istanti ma niente. In questo caso il fatto che la clipboard sia lockata è correttamente gestita dalla textbox.
Premete CTRL+C selezionando qualcosa nella griglia… Bam! Ecco l’eccezione.

3) Soluzione:

Le strade sono due. La prima è scrivere una classe che deriva da DataGrid e, nell’override del metodo OnExecutedCopy() gestisce l’eccezione (credo si possa fare, anche se non ne sono troppo sicuro). Il secondo è gestire l’eccezione direttamente in Application.DispatcherUnhandledException. Poiché la DataGrid l’avevo usata in più punti nell’applicativo, non sapevo se questo era un caso isolato o se da altre parti sorgesse lo stesso problema e #soprattutto# dato i tempi stretti, ho optato per la seconda con questo codice

var comEx = e.Exception as System.Runtime.InteropServices.COMException;
if (comEx != null)
{
    switch (comEx.ErrorCode)
    {
        case -2147221040: //OpenClipboard Failed (Exception from HRESULT: 0x800401D0 (CLIPBRD_E_CANT_OPEN))
            try
            {
                //Try get info about the application which lock the clipboard
                IntPtr handle = GetOpenClipboardWindow();
                if (handle != IntPtr.Zero)
                {
                    int capacity = GetWindowTextLength(new HandleRef(this, handle)) * 2;
                    StringBuilder stringBuilder = new StringBuilder(capacity);
                    GetWindowText(new HandleRef(this, handle), stringBuilder, stringBuilder.Capacity);

                    Trace.TraceWarning(string.Format("CLIPBOARD LOCKED BY {0}!", stringBuilder.ToString()));
                }
            }
            finally
            {
                e.Handled = true; //Handle Exception. No error to user. No crash of the application.    
            }
            return;
    }
}
Trace.TraceError(e.Exception.Message);

 

Questo è tutto.

4) Riflessioni:

Chissà quale programma causava il problema… ve lo dirò quando mi farò dare il log Smile (se mai l’avrò)

5) Considerazioni:

Se qualcuno volesse pingare il team di sviluppo per spiegare il problema faccia pure, oppure se volesse spiegarmi come fare contattatemi.

Print | posted on Tuesday, March 22, 2011 10:02 PM | Filed Under [ WPF workaround ]

Feedback

Gravatar

# re: WPF DataGrid Clipboard BUG(?)&Workaround

Even thesis service aren’t able create more great economics thesis relating with this topic or perhaps, you work for dissertation writing services. In that case I would buy theses from you directly.
9/18/2011 3:14 PM | AudraDorsey31
Gravatar

# re: WPF DataGrid Clipboard BUG(?)&Workaround

When different people are willing to get master degree on every field of science you would order dissertation discussion and thesis. I have done such things lots of times. I told just about dissertation writing service from research paper writing services.
9/28/2011 1:15 AM | HuffmanBILLIE24
Gravatar

# re: WPF DataGrid Clipboard BUG(?)&Workaround

Sometimes I try to make myself write research papers. Nevertheless, I I am not skilled enough to do this job. The one only way I do really see is to turn to essay writing services. I do not have a better thought.
9/29/2011 1:17 PM | SophiaJoyce33
Gravatar

# re: WPF DataGrid Clipboard BUG(?)&Workaround

There're a lot of stuff you do consequently. However, some of them can be pleasant and others will be worse. In fact, you don't like to do term papers writing, but, you will simply buy paper.
9/30/2011 4:25 AM | CandaceBrown24
Gravatar

# re: WPF DataGrid Clipboard BUG(?)&Workaround

Self education has to be the goal of haman’s life! For that goal, the writer job service provide the releases connecting to this post.
2/6/2012 8:29 PM | Cruz35Corine
Gravatar

# re: WPF DataGrid Clipboard BUG(?)&Workaround

The College Essay writing trainings can aid you make better your professionalism , which can be utilized for future academic career.
2/13/2012 4:34 AM | Political Essay

Post Comment

Title  
Name  
Email
Url
Comment   
Please add 5 and 7 and type the answer here:

Powered by:
Powered By Subtext Powered By ASP.NET