Streams
In order to provide similar programmatic interfaces to the broad range of I/O
devices with which a programmer has to contend, a stream-based architecture
was developed in .NET. I/O devices can be anything from printers to
hard disks to network interfaces.
Not all devices support the same functions. For example, it is possible
to read only the second half of a 1-Mb file, but not possible to download
only the second half of a Web page. Therefore, not all streams support the
same methods.
Properties such as
canRead()
,
canSeek()
, and
canWrite()
indicate the
capabilities of the stream when applied to a particular device
The most important stream in the context of this book is the
network-
Stream
, but another important stream is
fileStream
, which is used extensively
throughout this book to demonstrate file transfers over networks.
Streams can be used in two ways: asynchronously or synchronously.
When using a stream synchronously, upon calling a method, the thread will
halt until the operation is complete or fails. When using a stream asynchronously,
the thread will return from the method call immediately, and whenever
the operation is complete, a method will be called to signify the
completion of the operation, or some other event, such as I/O failure.
It is not user friendly to have a program “hang” when it is waiting for an
operation to complete. Therefore, synchronous method calls must be used
in a separate thread.
Through the use of threads and synchronous method calls, computers
achieve the illusion of being able to do several things at once. In reality,
most computers have only one central processing unit (CPU), and the illusion
is achieved by quickly switching between tasks every few milliseconds.
The following application illustrates the two techniques. The code in
this book will tend toward using synchronous streams, but it is important
to be able to recognize and understand asynchronous streams.
This code requires a little explanation. First, the magic number, 4096, is
simply a performance characteristic because it is quicker to transfer data
from disks in 4-Kb chunks than 1 byte at a time.
The final parameter in the
FileStream
constructor indicates whether
the operation is to be completed asynchronously or synchronously.
The most important thing to note is that there is no reference to
tbResults;
this implies that some other function must handle the data once
the read is complete. The
AsyncCallback
constructor refers to another function,
which is also referenced in the
BeginRead
method, so this must be it.
As you can see from the code, the
fs_StateChanged
function has not yet
been implemented. This function is called whenever the file is finished
reading.
Note:
Synchronous use of
FileStream
is more efficient when the file size is
less than 64 Kb and the file is located on the local machine.
This code doesn’t perform any file handling; instead, it creates a new
thread, whose entry point is the
syncRead
function. When this thread runs,
it does so in parallel with any other code that is running at the same time,
which includes the background operating system (OS) “housekeeping”
(Windows message handling) functions.
If the code above were replaced by a simple call to
syncRead()
, the program
would still operate; however, if the file happened to be several
gigabytes in size, the user would quickly perceive the application to be
“hung.” A hung application is notably nonresponsive and may turn white
when dragged behind another application. What is actually happening is
that the main thread of application is taking 100% processor time and
does not give the OS time to handle simple tasks such as redrawing the
user interface.
In certain time-critical applications, it may be necessary to take 100%
processor time, but any application with a user interface should remain
responsive at all times.
The next task is to implement the
syncRead
function:
In the above code, you will notice that the
FileStream
constructor is
enclosed in a
try/catch
block. This allows the program to recover gracefully
from problems such as a missing file or an unreadable disk. In realworld
applications, any operation that relies on the existence of files or network
resources should be contained within a
try/catch
block. This allows
programs to continue execution, even if something unexpected happens. In
most examples throughout this book,
try/catch
blocks are not used in
order to keep the examples concise and readable.
Three namespaces must be included in the code as follows:
Note:
The most concise way to read text files (under 1 Gb) is:
(new StreamReader(filename)).ReadToEnd();
Hibernate و Standby کردن کامپیوتر
' Hibernate
Application.SetSuspendState(PowerState.Hibernate, True, True)
'// Standby
Application.SetSuspendState(PowerState.Suspend, True, True)
شفاف کردن پشت زمینه ی کنترلها
control.BackColor = Color.FromArgb(trancparency, control.BackColor)
بستن یک پروسه در حال اجرا
System.Diagnostics.Process.Start("TaskKill", "/f /im Notepad.exe /t")
بدست آوردن مسیر دایرکتوری جاری برنامه
MessageBox.Show(Application.StartupPath)

نكته : درفايل ترتيبي امكان استفاده از ساختاروجود ندارد اين مورد درفايل تصادفي توضيح داده مي شود.
امادراين يادداشت به بررسي فايل ترتيبي مي پردازيم وفايل تصادفي رابه يادداشتي ديگرموكول مي كنيم.
حال مي خواهيم ببينيم چگونه مي توان يك فايل ترتيبي ايجاد كرد اطلاعاتي درداخل آن نوشت آطلاعات آنرا خواند وهمچنين چگونه مي توان آن فايل را حذف كرد.
ايجادكردن ويابازكردن يك فايل ترتيبي:
براي ايجاد كردن ويا بازكردن يك فايل ترتيبي ازدستور FileOpen استفاده مي كنيم.
اين دستور سه آرگومان دريافت مي كند:
1- درآرگومان اول بايد يك شماره به فايل خوداختصاص دهيد وازاين به بعد با استفاده ازآن شماره مي توان اطلاعات درون فايل را
خواند يا نوشت وهمچنين كارهاي ديگررا نيز با استفاده از آن شماره انجام
ادامه مطلب کلید کنید
Microsoft DAO 3.51 Object Libraryتیک دارمی کنیم.
بعد دو متغیر ازنوع تعریف می کنیم . Recordsetوdatabaseتعریف می کنیم .
(ادرس بانک اطلاعاتی)Set b = DBEngine.OpenDatabase
نکته:بااستفاده ازدستور app.pathمی توان آدرس برنامه را بدست آوردوبا چسباندن ان به نام بانک اطلاعاتی در رایانه های دیگرهم بدون تعغیر ادرس استفاده کرد.
(Set g = b.OpenRecordset("name table", dbOpenDynaset
dbOpenDynaset:باعث می شود جدول قابل تعغیر با شد
برای ارتبا ط ان با هر کنترلی ازخاصیت ()fieldsستفاده می کنیم مثلا
( شماره فیلد یا "نام فیلد")text1=g.fields
وبرای ذخیره کردن
g.addnew
g.fields=text
g.update
وبقیه خواص هایش مانند کنترل ado است