I have a winforms application with multiple GUI threads. I want them to be able to access other flow objects without having to separately track this information.
Is there a function in .NET so that I can process a winforms control or window object and return a stream? Or a function in the API that I can call for threadID?
(please, no comments saying that I have to do it differently ... also this does not apply to cross-flow operations).
Thanks!
Edit
For those of you who for some reason believed in my highlighted text, congratulations , you are hired !! Here is the problem:
"The application crashes into the wild, completely blocked, that is, it stops responding. Very intermittent and trying to debug it, it seems, never happens."
So what to do? Set an option in the program that the user can activate under our guidance, as a result of which, from another GUI thread in the same application, execute thread.abort in the main GUI thread, then we can look at the call stack in the error log. Viola, found it impossible to debug the error in less than a day. (Stop now, it had nothing to do with multithreaded abuse :-)
I admit that I almost did not ask about it, the reason I did it was to see the link to the object in the main form, but there was no thread. I give Chris Shine an a / c answer, this is a quick way, unfortunately, when the thread hangs, I would not be able to make a call (it also hangs). A little more digging revealed the GetWindowThreadProcessId API. But this is an unmanaged thread identifier, apparently, there are difficulties that turn this into a managed thread identifier.
So, I hit a bullet and put a global link to the main UI thread. I would post it for a start, but have not written it yet.
Now, if you have mercy on VB ...
In main open module / static class:
Public GUIThread As Threading.Thread Sub Main() '' // Create app main window ShellForm = New frmShell '' // Save main GUI Thread for abort routine GUIThread = Threading.Thread.CurrentThread If GetSetting("MyApp", "Testing", "CrashDebug", "False") = "True" Then '' // DO NOT run the pgm. like this normally - with try/catch around '' // Application.Run - or uncaught errors will kill the whole app!!! Try '' // This is the other of the 'Multiple GUI threads' I talked '' // about in the Orig Post. Dim t As New Threading.Thread(AddressOf StartCrashDebug) t.Start() Application.Run(ShellForm) Catch ex As Exception '' // This error routine passes errors off to another thread which '' // logs them (and also shows messages) MyLogError(ex, "CrashDebug - Main Window blew up") End Try Else '' // Normal mode - uncaught errors will get caught by UnhandledException, '' // logged, and Winforms will keep the GUI alive (since we _do_ care '' // more about users than computers right ;-) Application.Run(ShellForm) End If End Sub Sub StartCrashDebug() Dim f As New frmCrashFinder '' // Starting a window like this on a separate thread makes it 'Another '' // GUI thread' for winforms, by design Application.Run(f) End Sub
In the 'aborter WinForm:
Public Class frmCrashFinder Inherits Windows.Form Private Sub Abort_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Abort.Click GUIThread.Abort() End Sub End Class