mrpeardotnet / WinProdKeyFinder

Windows Product Key Finder written in C#.
http://www.mrpear.net
MIT License
133 stars 42 forks source link

Fix crash on copy clipboard #6

Closed visuve closed 5 years ago

visuve commented 5 years ago
mrpeardotnet commented 5 years ago

How is it crashing? I'd like to replicate the crash, just tested here original code and it works. Any specific environment?

visuve commented 5 years ago

It happens every time on Windows 10 no matter which .NET SDK you target the project. For example Windows 10 version 1709, build 16299.904, build debug, hit run, click copy to clipboard -> boom.

PhilipMur commented 5 years ago

Hello,

The problem is the timer not running from the main UI thread and causes this issue.

I have seen the problem myself just now. If you have disabled warning of cross thread calls in your debugger you wont see this issue.

mrpeardotnet add this line fro the timer instead.

void t_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { if(btnCopyToClipboar.InvokeRequired) { Invoke((MethodInvoker)delegate { btnCopyToClipboar.Text = CopyBtnText; btnCopyToClipboar.Enabled = true; }); } else { btnCopyToClipboar.Text = CopyBtnText; btnCopyToClipboar.Enabled = true; }

    }
PhilipMur commented 5 years ago

Hello visuve,

Always a bad idea to just Invoke something if it is not needed to be invoked as you block the UI thread without checking first.

visuve commented 5 years ago

@PhilipMur I did not know that, thanks. I will change the pull request.

visuve commented 5 years ago

Sorry for the update spam. Had some things wrong .gitconfig. Now there is a check for the InvokeRequired.

mrpeardotnet commented 5 years ago

Thank you @visuve and all other for the help and support, I've merged the pull request and made a new release.

PhilipMur commented 5 years ago

@Visue You are welcome, glad I could help out. @mrpeardotnet it must be the first time you have said thanks to anyone, the stars and planets must have all aligned ;-)

mrpeardotnet commented 5 years ago

@PhilipMur Sorry I do not get your comment about saying thanks, anything wrong?

combolek commented 3 years ago

The crash on copy to clipboard still happens but only in the Debug build when run in the debugger and only in the separate decode window after you paste the text from the registry and click "Parse DigitalProductId" and then "Copy key to clipboard" (i.e. not in the main "live OS" window).

I think there is code in DigitalProductIdForm.cs that probably still needs treatment similar to this PR. Here is the exception info from the debugger:

System.InvalidOperationException
  HResult=0x80131509
  Message=Cross-thread operation not valid: Control 'btnCopyToClipboard' accessed from a thread other than the thread it was created on.
  Source=System.Windows.Forms
  StackTrace:
   at System.Windows.Forms.Control.get_Handle()
   at System.Windows.Forms.Control.set_WindowText(String value)
   at System.Windows.Forms.Control.set_Text(String value)
   at WinProdKeyFind.DigitalProductIdForm.<btnCopyToClipboard_Click>b__7_0(Object o, ElapsedEventArgs args) in C:\data2\src\WinProdKeyFinder\WinProdKeyFind\DigitalProductIdForm.cs:line 106
   at System.Timers.Timer.MyTimerCallback(Object state)

  This exception was originally thrown at this call stack:
    [External Code]
    WinProdKeyFind.DigitalProductIdForm.btnCopyToClipboard_Click.AnonymousMethod__7_0(object, System.Timers.ElapsedEventArgs) in DigitalProductIdForm.cs
    [External Code]