Update The following article is one way to to achieve threading…But an easier way to try first is the BackGround Worker component (How to: Run an Operation in the Background). The below article shows how to invoke back to the form which is a step one does not have to do directly with the background worker due to its design.
Whenever one has threads or timers doing work on a winform application there is a need to update the screen from those operations. Most first time winform programmers fall into the trap where they try to update a screen control from those background processes and discover that their app locks up. The reason is that any data screen updating needs to be done by the GUI thread solely and not from any background task.
What the code should do is invoke a delegate back to the GUI thread to do the screen updating. This article shows one such way.
Step 1 define a delegate on the winform form class, and have an event derived from it which can be subscribed to such as:
// This is the format the delegate method
// will use when invoking back to the main gui
// thread.
publicdelegatevoid DataAcquired( object sender );
// The main thread will check for events
// invoked by subscribed threads here.
// This is the subscription point for the
// threads.
publicevent DataAcquired OnDataAcquiredEvent;
In the above example we are just using an object to pass between the threads, but you can specify anything you want which is dictated by your circumstances.
Step 2 then have the form subscribe to the event (as in the constructor) which will be eventually be consumed by the worker threads or timers.
OnDataAcquiredEvent +=
new DataAcquired(ThreadReportsDataAquiredEvent);
Note intellisense will do the above steps for you after the +=, accept them and it will do the next step
Step 3 Create the method that will handle the GUI update event
Why not have your application provide information to its running state such as handles, threads and the bytes being used. This article will show you how to create a Winform Application with a status bar where each label reports handles, threads and the bytes used.
Steps:
Create a winform application.
Add the Timer component to the form and set these properties:
Timer: Change the Interval from 100 to 1000 so it ticks off every second.
Timer: Set Enabled to True
Create a event for the Tick event.
Add three PerfomanceCounter Components to the form and name them pcPrivateBytes, pcHandles and pcThreads. Build the application and run it.Then set the properties below:
All: CategoryName = Process
All: InstanceLifetime = Global
All: MachineName = . (Yes a period)
All: InstanceName = (Find the running application name in the drop down list, hence why we are running it.)
pcHandles: CounterName = Handle Count
pcThreads: CounterName = Thread Count
pcPrivateBytes: CounterName = Private Bytes(Private bytes is only the bytes that the application allocates and not any extraneous bytes allocated by the system)
Close the running application.
Add a Status Strip to the application and create three status label areas named tsslHandles and tsslThreads, tsslPrivateBytes. Note the Private bytes is pretty dynamic, so placing it at the end is a good idea.
In the timer tick event add this code which will get the current values of the performance moniter:
tsslThread.Text = string.Format("Threads: {0}", pcThreads.NextValue().ToString());
tsslHandles.Text = string.Format("Handles: {0}", pcHandles.NextValue().ToString());
// Divide the return of Private Bytes to something we will recognize.
tsslPrivateBytes.Text = string.Format("Private Bytes: {0}", (pcPrivateBytes.NextValue() / 1000).ToString());
Run the application and you should see this which gives you a running total of what your application is doing.
(Updated 02/14/2012 Code Examples use SyntaxHighlighter)
The goal is to having a working application which hides itself not to the taskbar but to an icon in the system tray. This is valid in .Net 2 and above. Here are the steps
Create a Winform Application.
Bring up the mainform.
Drag a NotifyIcon component to the form.
NotifyIcon Setup
In the properties set the icon to use. Note if you don’t have one grab the root.ico from the Visual Studio folder to use.
We want to hide the icon initially so set the Visible property to false.
At this point compile and run. The application starts of as normal with a taskbar button but no icon. When the application is minimized it goes to the tray icon and there is no taskbar button. When the user clicks on the icon in the tray, it is returned to its previous state and the icon is removed.