Visual Basic specifies a Boolean’s default values as zero for False and -1 for True. You may save and set the value of a check box, based on the absolute value of a Boolean variable, as these correspond to the intrinsic constants vbUnchecked and vbChecked:
Read the rest of this entry »
Popularity: 13% [?]
Unlike the Windows 95 common controls, the standard list box doesn’t have a horizontal scrollbar when list items are too wide to fit within the list box. Fortunately, it’s not hard to direct a listbox control to display a horizontal scrollbar. Add this code to a form’s Load event. It fills a list box with 100 long strings and calls SetHScroll to show a horizontal scrollbar in the list box:
Private Sub Form_Load() Dim i As Integer For i = 1 To 100 List1.AddItem CStr(i) & _ " bottle(s) of beer on the wall." Next i SetHScroll Me, List1, List1.List(0) End Sub
Popularity: 14% [?]
Setting an element in an array of option buttons to True is easy. Click on the option button, or use this code:
OptionArray(ThisOne) = True
ThisOne is the Index of the member you want to be selected. Getting the True member of an option array is not so simple. Because you need to perform this kind of task in almost any reasonably complex program, adding this function to your code library or generic module simplifies the task. You don’t need to know the array size in advance:
Read the rest of this entry »
Popularity: 13% [?]
When attempting to determine which option buttons a user has selected from an array of option buttons, use this code instead of using an inefficient If-Then construct:
intSelectedItem = Option(0).Value * O - _
Option(1).Value*1 - Option(2).Value * 2
Popularity: 13% [?]
For threads that are manually created via the Thread class, you can call the Join method to wait for a thread to finish. This works well when you need to wait for all threads to finish processing before an application terminates. Unfortunately, the thread pool threads do not have a Join method. You need to make sure that all threads in the thread pool have finished processing before another thread terminates or your application’s main thread terminates.
Use a combination of the ThreadPool methods — GetMaxThreads and GetAvailableThreads — to determine when the ThreadPool is finished processing the requests.
Read the rest of this entry »
Popularity: 34% [?]
Synchronizing the Reading and Writing of a Resource Efficiently
When you have a resource that is shared by multiple threads, you need to provide exclusive access to this resource when a thread is writing to it. However, you do not want the overhead of providing exclusive access to this resource when multiple threads are only reading from it. You want to allow one thread to access a shared resource only if it is writing to it, but you also want to allow multiple threads to read from this resource. While multiple threads can read from a resource, a write operation cannot occur while any thread is reading from this resource.
Example below uses the ReaderWriterLock class from the System.Threading. The ReaderWriterLock is optimized for scenarios where you have data that changes infrequently but needs protection for those times when it is updated in a multithreading scenario. To illustrate, the GradeBoard class represents a board where an instructor will post the grades students received from a class. Many students can read the grade board, but only the instructor can post a grade (write) to the grade board. Students will not, however, be able to read from the board while the instructor is updating it.
Read the rest of this entry »
Popularity: 29% [?]
The ThreadPool is a great way to perform background tasks without having to manage all aspects of the thread yourself. It can be handy to know when the ThreadPool itself is going to become a bottleneck to your application, and the GetAvailableThreads method can help you. However, you might want to check your application design if you are consistently using this many threads as you might be losing performance due to contention or context switching. Queuing up work when the ThreadPoolM is full simply queues it up for execution once one of the threads comes free; the request isn’t lost, just postponed.
In this example, your application will be creating many threads from the thread pool. When creating a thread from this pool, you want to be informed as to whether a thread in the pool is available or if none are available, and the request for a new thread will have to be queued. Basically, you want to know whether a thread is available for immediate use from the thread pool. Use the ThreadPool.GetAvailableThreads method to get the number of worker threads currently available in the ThreadPool to determine whether you should queue another request to launch another thread via ThreadPool.QueueUserWorkItem or take an alternate action. The Main method calls a method (SpawnManyThreads) to spawn lots of threads to do work in the ThreadPool, then waits for a bit to simulate processing.
Read the rest of this entry »
Popularity: 43% [?]
When using a System.Threading.Timers.Timer class, here are a few scenarios you may encounter:
There are two types of Timer classes in .NET Framework, one from System.Threading and another one from System.Windows.Forms. The former timer is enough to serve the earlier scenarios, but if you are doing UI work and want to use timers, you should investigate the System.Windows.Forms.Timer class. If you are doing server work, you might also want to look at System.Threading.Timers.Timer as well. Both of these classes add events for when the timers are disposed and when the timer “ticks”; they also add properties that expose the settings. Here we look at the Timer class of System.Threading.Timers sample codes.
Read the rest of this entry »
Popularity: 26% [?]