When creating multithreading application, thread-safe access through accessor functions to an internal member variable is very important. Poorly written code may cause the application behave improperly, deadlock, etc. This article shows an example of on how to create a thread-safe access class. To achieve it, one way is the use the lock keyword to make a block of code as critical section. This keyword accepts a parameter of either the type object for the class (such as typeof(MyClass)) or a class instance object (new MyClass()). It uses this type or object to control what you are locking.
Read the rest of this entry »
Popularity: 34% [?]
This article is similar to the previous one Finding Members in an Assembly. This time, use the same technique as previouly, but filter out all types except interfaces. This article also makes use of the GetMember method of the System.Type class. The name may contain a * wildcard character at the end of the string only. If the * wildcard character is the only character in the name parameter, all members are returned.
If you’d like to do a case-sensitive search, you can omit the BindingFlags.IgnoreCase flag from the call to Type.GetMember.
Read the rest of this entry »
Popularity: 3% [?]
The GetMember method of the System.Type class is useful for finding one or more methods within a type. This method returns an array of MemberInfo objects that describe any members that match the given parameters.
The * character may be used as a wildcard character only at the end of the name parameter string. In addition, it may be the only character in the name parameter; if this is so, all members are returned. No other wildcard characters, such as ?, are supported.
Read the rest of this entry »
Popularity: 3% [?]
If you want to write some code to download data a location specified by a URI, simply you can use the WebClient DownloadData and DownloadFile methods; this data can be either an array of bytes or a file. WebClient simplifies downloading of files and bytes in files, as these are common tasks when dealing with the Web. The more traditional stream-based method for downloading can also be accessed via the OpenRead method on the WebClient.
Read the rest of this entry »
Popularity: 2% [?]
Obtaining the exported types in an assembly is useful when determining the public interface to that assembly. This ability can greatly aid in learning to use a new assembly or can aid the developer of that assembly in determining all access points to their assembly and seeing whether they are adequately secure from malicious code. To get these exported types, we use the GetExportedTypes method on the System.Reflection.Assembly type. The exported types consist of all of the types that are publicly accessible from outside of the assembly. A type may have public accessibility but not be accessible from outside of the assembly. Read the rest of this entry »
Popularity: 2% [?]
If a file is opened within your application and the FileShare parameter of the FileStream.Open call is set to FileShare.ReadWrite or FileShare.Write, other code in your application can alter the contents of the file while you are using it. To handle file access with more granularity, use the Lock method on the FileStream object to prevent other code from overwriting all or a portion of your file. Once you are done with the locked portion of your file, you can call the Unlock method on the FileStream object to allow other code in your application to write data to the file or that portion of the file.
Read the rest of this entry »
Popularity: 5% [?]
Say we needed to drive the CMD.EXE application to display the current time with the TIME /T command (it is possible to just run this command from the command line, but this way we can demonstrate an alternative method to drive an application that responds to standard input). The way to do this is to launch a process that is looking for input on the standard input stream. This is accomplished via the Process class StartInfo property, which is an instance of a ProcessStartInfo class. The Process.Start method will launch a new process, but the StartInfo property controls many of the details of what sort of environment that process executes in.
First, make sure that the StartInfo.RedirectStandardInput property is set to true. This setting notifies the process that it should read from standard input. Then set the StartInfo.UseShellExecute property to false because if you were to let the shell launch the process for you, it would prevent you from redirecting standard input.
Read the rest of this entry »
Popularity: 3% [?]
There is always time to use a temporary file in the application. This file will exist only as long as the process that created it remains running.
Use the static GetTempPath and GetTempFileName methods on the Path class.
The Path class provides two methods for working with temporary files. The first is the static GetTempPath method, which returns the path to the temporary directory specified in the TEMP environment variable.
The second static method, GetTempFileName, will automatically generate a temporary filename, create a zero-length file in the user’s temporary directory, and return a string containing this filename and its path.
Read the rest of this entry »
Popularity: 3% [?]