印刷キューは、常に 24 時間使用できるわけではありません。 開始時刻と終了時刻のプロパティがあり、特定の時刻に使用できないように設定できます。 たとえば、この機能を使用して、午後 5 時以降に特定の部門の専用プリンターを予約できます。 その部門は、他の部門が使用するのとは異なるキューでプリンターを処理します。 他の部門のキューは午後 5 時以降は使用できないように設定され、優先部門のキューは常に使用可能に設定できます。
さらに、印刷ジョブ自体は、指定された時間内にのみ印刷可能に設定できます。
Microsoft .NET Framework の API で公開されている PrintQueue クラスと PrintSystemJobInfo クラスは、特定の印刷ジョブが現在の時点で特定のキューに印刷できるかどうかをリモートで確認する手段を提供します。
例
次の例は、印刷ジョブに関する問題を診断できるサンプルです。
この種の関数には、次の 2 つの主要な手順があります。
StartTimeOfDay の UntilTimeOfDay プロパティと PrintQueue プロパティを読み取り、現在の時刻がそれらの間にあるかどうかを判断します。
StartTimeOfDay の UntilTimeOfDay プロパティと PrintSystemJobInfo プロパティを読み取り、現在の時刻がそれらの間にあるかどうかを判断します。
しかし、これらのプロパティが DateTime オブジェクトではないという事実から複雑な問題が発生します。 代わりに、時刻を午前 0 時からの分数として表す Int32 オブジェクトです。 さらに、これは現在のタイム ゾーンの午前 0 時ではなく、UTC (協定世界時) の午前 0 時です。
最初のコード例では、静的メソッド ReportQueueAndJobAvailabilityを示します。このメソッドには PrintSystemJobInfo が渡され、ヘルパーメソッドを呼び出して、ジョブが現在の時刻に印刷できるかどうかを確認し、印刷できない場合はいつ印刷できるかを決定します。 PrintQueue がメソッドに渡されないことに注意してください。 これは、PrintSystemJobInfo の HostingPrintQueue プロパティにキューへの参照が含まれているためです。
下位メソッドには、パラメーターとして または PrintQueue を受け取ることができるオーバーロードされた PrintSystemJobInfo メソッドが含まれます。 TimeConverter.ConvertToLocalHumanReadableTime もあります。 これらのメソッドはすべて、以下で説明します。
ReportQueueAndJobAvailability メソッドは、この時点でキューまたは印刷ジョブが使用できないかどうかを確認することから始まります。 どちらかが使用できない場合は、キューが使用できないかどうかを確認します。 使用できない場合、メソッドはこの事実とキューが再び使用可能になる時刻を報告します。 次にジョブを確認し、使用できない場合は、次に印刷できる時間帯を報告します。 最後に、このメソッドは、ジョブが印刷できる最も早い時刻を報告します。 これは、次の 2 つの時刻の遅い方です。
印刷キューが次に使用可能になった時刻。
印刷ジョブが次に使用可能になる時刻。
1 日の時刻を報告する場合、ToShortTimeString メソッドも呼び出されます。このメソッドは出力から年、月、日を抑制するためです。 印刷キューまたは印刷ジョブの可用性を特定の年、月、または日に制限することはできません。
static void ReportQueueAndJobAvailability (PrintSystemJobInfo^ theJob)
{
if (!(ReportAvailabilityAtThisTime(theJob->HostingPrintQueue) && ReportAvailabilityAtThisTime(theJob)))
{
if (!ReportAvailabilityAtThisTime(theJob->HostingPrintQueue))
{
Console::WriteLine("\nThat queue is not available at this time of day." + "\nJobs in the queue will start printing again at {0}", TimeConverter::ConvertToLocalHumanReadableTime(theJob->HostingPrintQueue->StartTimeOfDay).ToShortTimeString());
// TimeConverter class is defined in the complete sample
}
if (!ReportAvailabilityAtThisTime(theJob))
{
Console::WriteLine("\nThat job is set to print only between {0} and {1}", TimeConverter::ConvertToLocalHumanReadableTime(theJob->StartTimeOfDay).ToShortTimeString(), TimeConverter::ConvertToLocalHumanReadableTime(theJob->UntilTimeOfDay).ToShortTimeString());
}
Console::WriteLine("\nThe job will begin printing as soon as it reaches the top of the queue after:");
if (theJob->StartTimeOfDay > theJob->HostingPrintQueue->StartTimeOfDay)
{
Console::WriteLine(TimeConverter::ConvertToLocalHumanReadableTime(theJob->StartTimeOfDay).ToShortTimeString());
} else
{
Console::WriteLine(TimeConverter::ConvertToLocalHumanReadableTime(theJob->HostingPrintQueue->StartTimeOfDay).ToShortTimeString());
}
}
};
internal static void ReportQueueAndJobAvailability(PrintSystemJobInfo theJob)
{
if (!(ReportAvailabilityAtThisTime(theJob.HostingPrintQueue) && ReportAvailabilityAtThisTime(theJob)))
{
if (!ReportAvailabilityAtThisTime(theJob.HostingPrintQueue))
{
Console.WriteLine("\nThat queue is not available at this time of day." +
"\nJobs in the queue will start printing again at {0}",
TimeConverter.ConvertToLocalHumanReadableTime(theJob.HostingPrintQueue.StartTimeOfDay).ToShortTimeString());
// TimeConverter class is defined in the complete sample
}
if (!ReportAvailabilityAtThisTime(theJob))
{
Console.WriteLine("\nThat job is set to print only between {0} and {1}",
TimeConverter.ConvertToLocalHumanReadableTime(theJob.StartTimeOfDay).ToShortTimeString(),
TimeConverter.ConvertToLocalHumanReadableTime(theJob.UntilTimeOfDay).ToShortTimeString());
}
Console.WriteLine("\nThe job will begin printing as soon as it reaches the top of the queue after:");
if (theJob.StartTimeOfDay > theJob.HostingPrintQueue.StartTimeOfDay)
{
Console.WriteLine(TimeConverter.ConvertToLocalHumanReadableTime(theJob.StartTimeOfDay).ToShortTimeString());
}
else
{
Console.WriteLine(TimeConverter.ConvertToLocalHumanReadableTime(theJob.HostingPrintQueue.StartTimeOfDay).ToShortTimeString());
}
}//end if at least one is not available
}//end ReportQueueAndJobAvailability
Friend Shared Sub ReportQueueAndJobAvailability(ByVal theJob As PrintSystemJobInfo)
If Not(ReportAvailabilityAtThisTime(theJob.HostingPrintQueue) AndAlso ReportAvailabilityAtThisTime(theJob)) Then
If Not ReportAvailabilityAtThisTime(theJob.HostingPrintQueue) Then
Console.WriteLine(vbLf & "That queue is not available at this time of day." & vbLf & "Jobs in the queue will start printing again at {0}", TimeConverter.ConvertToLocalHumanReadableTime(theJob.HostingPrintQueue.StartTimeOfDay).ToShortTimeString())
' TimeConverter class is defined in the complete sample
End If
If Not ReportAvailabilityAtThisTime(theJob) Then
Console.WriteLine(vbLf & "That job is set to print only between {0} and {1}", TimeConverter.ConvertToLocalHumanReadableTime(theJob.StartTimeOfDay).ToShortTimeString(), TimeConverter.ConvertToLocalHumanReadableTime(theJob.UntilTimeOfDay).ToShortTimeString())
End If
Console.WriteLine(vbLf & "The job will begin printing as soon as it reaches the top of the queue after:")
If theJob.StartTimeOfDay > theJob.HostingPrintQueue.StartTimeOfDay Then
Console.WriteLine(TimeConverter.ConvertToLocalHumanReadableTime(theJob.StartTimeOfDay).ToShortTimeString())
Else
Console.WriteLine(TimeConverter.ConvertToLocalHumanReadableTime(theJob.HostingPrintQueue.StartTimeOfDay).ToShortTimeString())
End If
End If 'end if at least one is not available
End Sub
ReportAvailabilityAtThisTime メソッドの 2 つのオーバーロードは、渡される型を除いて同じであるため、以下に PrintQueue バージョンのみが表示されます。
注
型を除いてメソッドが同一であるという事実は、サンプルが ReportAvailabilityAtThisTime
(下記のコード例に示されている) ReportAvailabilityAtThisTime メソッドは、まず Boolean sentinel 変数を true
に初期化します。 キューが使用できない場合は、false
にリセットされます。
次に、メソッドは開始時間と "until" 時間が同じかどうかを確認します。 存在する場合、キューは常に使用可能であるため、メソッドは true
返します。
キューが常に使用できない場合、メソッドは静的な UtcNow プロパティを使用して、現在の時刻を DateTime オブジェクトとして取得します。 (StartTimeOfDay プロパティと UntilTimeOfDay プロパティ自体が UTC 時刻であるため、現地時刻は必要ありません)。
ただし、これら2つのプロパティはDateTimeオブジェクトではありません。 これらは UTC の午前 0 時からの分数として時刻を表す Int32 です。 そのため、DateTime オブジェクトを午前 0 時からの分数に変換する必要があります。 これが完了すると、メソッドは単に "now" がキューの開始時刻と "until" 時刻の間にあるかどうかを確認し、"now" が 2 回の間でない場合は sentinel を false に設定し、Sentinel を返します。
static Boolean ReportAvailabilityAtThisTime (PrintQueue^ pq)
{
Boolean available = true;
if (pq->StartTimeOfDay != pq->UntilTimeOfDay)
{
DateTime utcNow = DateTime::UtcNow;
Int32 utcNowAsMinutesAfterMidnight = (utcNow.TimeOfDay.Hours * 60) + utcNow.TimeOfDay.Minutes;
// If now is not within the range of available times . . .
if (!((pq->StartTimeOfDay < utcNowAsMinutesAfterMidnight) && (utcNowAsMinutesAfterMidnight < pq->UntilTimeOfDay)))
{
available = false;
}
}
return available;
};
private static Boolean ReportAvailabilityAtThisTime(PrintQueue pq)
{
Boolean available = true;
if (pq.StartTimeOfDay != pq.UntilTimeOfDay) // If the printer is not available 24 hours a day
{
DateTime utcNow = DateTime.UtcNow;
Int32 utcNowAsMinutesAfterMidnight = (utcNow.TimeOfDay.Hours * 60) + utcNow.TimeOfDay.Minutes;
// If now is not within the range of available times . . .
if (!((pq.StartTimeOfDay < utcNowAsMinutesAfterMidnight)
&&
(utcNowAsMinutesAfterMidnight < pq.UntilTimeOfDay)))
{
available = false;
}
}
return available;
}//end ReportAvailabilityAtThisTime
Private Shared Function ReportAvailabilityAtThisTime(ByVal pq As PrintQueue) As Boolean
Dim available As Boolean = True
If pq.StartTimeOfDay <> pq.UntilTimeOfDay Then ' If the printer is not available 24 hours a day
Dim utcNow As Date = Date.UtcNow
Dim utcNowAsMinutesAfterMidnight As Int32 = (utcNow.TimeOfDay.Hours * 60) + utcNow.TimeOfDay.Minutes
' If now is not within the range of available times . . .
If Not((pq.StartTimeOfDay < utcNowAsMinutesAfterMidnight) AndAlso (utcNowAsMinutesAfterMidnight < pq.UntilTimeOfDay)) Then
available = False
End If
End If
Return available
End Function 'end ReportAvailabilityAtThisTime
TimeConverter.ConvertToLocalHumanReadableTime メソッド (以下のコード例で示します) では、Microsoft .NET Framework で導入されたメソッドは使用されないため、簡単に説明します。 このメソッドには二重変換タスクがあります。午前 0 時より後の分を表す整数を使用し、人間が判読できる時刻に変換し、これを現地時刻に変換する必要があります。 これを実現するには、最初に UTC の午前 0 時に設定された DateTime オブジェクトを作成してから、AddMinutes メソッドを使用してメソッドに渡された分数を追加します。 これにより、メソッドに渡された元の時刻を表す新しい DateTime が返されます。 その後、ToLocalTime メソッドはこれを現地時刻に変換します。
private ref class TimeConverter {
internal:
static DateTime ConvertToLocalHumanReadableTime (Int32 timeInMinutesAfterUTCMidnight)
{
// Construct a UTC midnight object.
// Must start with current date so that the local Daylight Savings system, if any, will be taken into account.
DateTime utcNow = DateTime::UtcNow;
DateTime utcMidnight = DateTime(utcNow.Year, utcNow.Month, utcNow.Day, 0, 0, 0, DateTimeKind::Utc);
// Add the minutes passed into the method in order to get the intended UTC time.
Double minutesAfterUTCMidnight = ((Double)timeInMinutesAfterUTCMidnight);
DateTime utcTime = utcMidnight.AddMinutes(minutesAfterUTCMidnight);
// Convert to local time.
DateTime localTime = utcTime.ToLocalTime();
return localTime;
};
};
class TimeConverter
{
// Convert time as minutes past UTC midnight into human readable time in local time zone.
internal static DateTime ConvertToLocalHumanReadableTime(Int32 timeInMinutesAfterUTCMidnight)
{
// Construct a UTC midnight object.
// Must start with current date so that the local Daylight Savings system, if any, will be taken into account.
DateTime utcNow = DateTime.UtcNow;
DateTime utcMidnight = new DateTime(utcNow.Year, utcNow.Month, utcNow.Day, 0, 0, 0, DateTimeKind.Utc);
// Add the minutes passed into the method in order to get the intended UTC time.
Double minutesAfterUTCMidnight = (Double)timeInMinutesAfterUTCMidnight;
DateTime utcTime = utcMidnight.AddMinutes(minutesAfterUTCMidnight);
// Convert to local time.
DateTime localTime = utcTime.ToLocalTime();
return localTime;
}// end ConvertToLocalHumanReadableTime
}//end TimeConverter class
Friend Class TimeConverter
' Convert time as minutes past UTC midnight into human readable time in local time zone.
Friend Shared Function ConvertToLocalHumanReadableTime(ByVal timeInMinutesAfterUTCMidnight As Int32) As Date
' Construct a UTC midnight object.
' Must start with current date so that the local Daylight Savings system, if any, will be taken into account.
Dim utcNow As Date = Date.UtcNow
Dim utcMidnight As New Date(utcNow.Year, utcNow.Month, utcNow.Day, 0, 0, 0, DateTimeKind.Utc)
' Add the minutes passed into the method in order to get the intended UTC time.
Dim minutesAfterUTCMidnight As Double = CType(timeInMinutesAfterUTCMidnight, Double)
Dim utcTime As Date = utcMidnight.AddMinutes(minutesAfterUTCMidnight)
' Convert to local time.
Dim localTime As Date = utcTime.ToLocalTime()
Return localTime
End Function ' end ConvertToLocalHumanReadableTime
End Class
こちらも参照ください
.NET Desktop feedback