Closed visuve closed 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?
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.
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; }
}
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.
@PhilipMur I did not know that, thanks. I will change the pull request.
Sorry for the update spam. Had some things wrong .gitconfig. Now there is a check for the InvokeRequired.
Thank you @visuve and all other for the help and support, I've merged the pull request and made a new release.
@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 ;-)
@PhilipMur Sorry I do not get your comment about saying thanks, anything wrong?
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]