FordGT90Concept
"I go fast!1!11!1!"
- Joined
- Oct 13, 2008
- Messages
- 26,259 (4.63/day)
- Location
- IA, USA
System Name | BY-2021 |
---|---|
Processor | AMD Ryzen 7 5800X (65w eco profile) |
Motherboard | MSI B550 Gaming Plus |
Cooling | Scythe Mugen (rev 5) |
Memory | 2 x Kingston HyperX DDR4-3200 32 GiB |
Video Card(s) | AMD Radeon RX 7900 XT |
Storage | Samsung 980 Pro, Seagate Exos X20 TB 7200 RPM |
Display(s) | Nixeus NX-EDG274K (3840x2160@144 DP) + Samsung SyncMaster 906BW (1440x900@60 HDMI-DVI) |
Case | Coolermaster HAF 932 w/ USB 3.0 5.25" bay + USB 3.2 (A+C) 3.5" bay |
Audio Device(s) | Realtek ALC1150, Micca OriGen+ |
Power Supply | Enermax Platimax 850w |
Mouse | Nixeus REVEL-X |
Keyboard | Tesoro Excalibur |
Software | Windows 10 Home 64-bit |
Benchmark Scores | Faster than the tortoise; slower than the hare. |
I ran into a little nucience of .NET today: hiding a form from within a form. It should be really simple like this:
You'd think it would work, but it doesn't. Handles (which .Hide() uses) don't work until the form leaves the constructor.
So, how to work around this? Make a second thread to order the form to hide itself, of course! Here's the important code:
Basically, all the above code does is create a "helper" thread which tells our form thread that it needs to hide. The form thread won't allow this so we have to make the form thread tell itself it needs to hide.
This isn't as good as not creating the form in the first place but it is a quick fix. You will see the form blash for less than a second before it hides.
If you want more information about how this works, do some searching on multi-threaded programming in C#. This trick can also be done in VB.NET.
Note: This method does not appear to work in Server 2003.
Code:
public Main()
{
InitializeComponent();
this.Hide();
}
So, how to work around this? Make a second thread to order the form to hide itself, of course! Here's the important code:
Code:
public delegate void AutomaticHideHandler(); // This is what we trigger and place on the event stack.
public class Main : Form
{
public event AutomaticHideHandler AutomaticHide; // This holds all triggered events.
private Thread _AutoHide = null; // This makes sure the thread is alive (in the memory) as long as we need it.
public Main()
{
AutomaticHide += new AutomaticHideHandler(Main_AutomaticHide); // Add an instance of the of the event to the event stack.
InitializeComponent(); // This initializes the form.
_AutoHide = new Thread(Main_AutomaticHide); // This creates a new thread and saves it into our class variable.
_AutoHide.Start(); // This starts the thread we just created.
}
private void Main_AutomaticHide()
{
// Because another thread is going to call this method, we have to catch the cross-threaded reference before it happens.
if (this.InvokeRequired)
{
this.Invoke(new AutomaticHideHandler(AutomaticHide)); // Make the form's thread retrigger the event
_AutoHide = null; // Free up the memory from _AutoHide which is no longer needed.
}
else
this.Hide(); // Finally hide the form.
}
}
Basically, all the above code does is create a "helper" thread which tells our form thread that it needs to hide. The form thread won't allow this so we have to make the form thread tell itself it needs to hide.
This isn't as good as not creating the form in the first place but it is a quick fix. You will see the form blash for less than a second before it hides.
If you want more information about how this works, do some searching on multi-threaded programming in C#. This trick can also be done in VB.NET.
Note: This method does not appear to work in Server 2003.
Last edited: