-
Notifications
You must be signed in to change notification settings - Fork 5k
Nullable annotation for System.Windows.Extensions #57896
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
stephentoub
merged 12 commits into
dotnet:main
from
huoyaoyuan:system-windows-extensions
Nov 24, 2021
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
66e021d
Enable nullable in project file.
huoyaoyuan 00fcf07
Annotate SoundPlayer
huoyaoyuan 52898fd
Annotate remaining types
huoyaoyuan fabeb33
Update ref source
huoyaoyuan de15658
Events should be nullable
huoyaoyuan 4e1f870
Fix assertion for NRE
huoyaoyuan 22d98fd
Update nullability of SoundLocation.
huoyaoyuan 599cdce
Adjust nullability of SoundLocation and Stream.
huoyaoyuan f810312
Title and message should be nullable
huoyaoyuan 8b7d013
Merge branch 'main'
huoyaoyuan 689255c
Fix new NRT issue
huoyaoyuan 9319514
Merge branch 'main'
huoyaoyuan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
src/libraries/System.Windows.Extensions/ref/System.Windows.Extensions.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
|
||
using System.ComponentModel; | ||
using System.Diagnostics; | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.IO; | ||
using System.Net; | ||
using System.Runtime.InteropServices; | ||
|
@@ -18,7 +19,7 @@ public class SoundPlayer : Component, ISerializable | |
private const int BlockSize = 1024; | ||
private const int DefaultLoadTimeout = 10000; // 10 secs | ||
|
||
private Uri _uri; | ||
private Uri? _uri; | ||
private string _soundLocation = string.Empty; | ||
private int _loadTimeout = DefaultLoadTimeout; | ||
|
||
|
@@ -28,16 +29,16 @@ public class SoundPlayer : Component, ISerializable | |
// the worker copyTask | ||
// we start the worker copyTask ONLY from entry points in the SoundPlayer API | ||
// we also set the tread to null only from the entry points in the SoundPlayer API | ||
private Task _copyTask; | ||
private CancellationTokenSource _copyTaskCancellation; | ||
private Task? _copyTask; | ||
private CancellationTokenSource? _copyTaskCancellation; | ||
|
||
// local buffer information | ||
private int _currentPos; | ||
private Stream _stream; | ||
private Exception _lastLoadException; | ||
private Stream? _stream; | ||
private Exception? _lastLoadException; | ||
private bool _doesLoadAppearSynchronous; | ||
private byte[] _streamData; | ||
private AsyncOperation _asyncOperation; | ||
private byte[]? _streamData; | ||
private AsyncOperation? _asyncOperation; | ||
private readonly SendOrPostCallback _loadAsyncOperationCompleted; | ||
|
||
// event | ||
|
@@ -55,7 +56,7 @@ public SoundPlayer(string soundLocation) : this() | |
SetupSoundLocation(soundLocation ?? string.Empty); | ||
} | ||
huoyaoyuan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
public SoundPlayer(Stream stream) : this() | ||
public SoundPlayer(Stream? stream) : this() | ||
{ | ||
_stream = stream; | ||
} | ||
|
@@ -99,7 +100,7 @@ public string SoundLocation | |
} | ||
} | ||
|
||
public Stream Stream | ||
public Stream? Stream | ||
{ | ||
get | ||
{ | ||
|
@@ -126,7 +127,7 @@ public Stream Stream | |
|
||
public bool IsLoadCompleted { get; private set; } | ||
|
||
public object Tag { get; set; } | ||
public object? Tag { get; set; } | ||
|
||
public void LoadAsync() | ||
{ | ||
|
@@ -161,9 +162,9 @@ public void LoadAsync() | |
LoadStream(false); | ||
} | ||
|
||
private void LoadAsyncOperationCompleted(object arg) | ||
private void LoadAsyncOperationCompleted(object? arg) | ||
{ | ||
OnLoadCompleted((AsyncCompletedEventArgs)arg); | ||
OnLoadCompleted((AsyncCompletedEventArgs)arg!); | ||
} | ||
|
||
// called for loading a stream synchronously | ||
|
@@ -227,6 +228,7 @@ private void LoadAndPlay(int flags) | |
else | ||
{ | ||
LoadSync(); | ||
Debug.Assert(_streamData != null); | ||
ValidateSoundData(_streamData); | ||
Interop.WinMM.PlaySound(_streamData, IntPtr.Zero, Interop.WinMM.SND_MEMORY | Interop.WinMM.SND_NODEFAULT | flags); | ||
} | ||
|
@@ -271,7 +273,9 @@ private void LoadSync() | |
_stream = webResponse.GetResponseStream(); | ||
} | ||
|
||
if (_stream.CanSeek) | ||
// DO NOT assert - NRE is expected for null stream | ||
// See SoundPlayerTests.Load_NullStream_ThrowsNullReferenceException | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It'd be worth opening an issue for this to decide separately whether we should throw a better exception in that case. |
||
if (_stream!.CanSeek) | ||
{ | ||
// if we can get data synchronously, then get it | ||
LoadStream(true); | ||
|
@@ -304,6 +308,7 @@ private void LoadSync() | |
|
||
private void LoadStream(bool loadSync) | ||
{ | ||
Debug.Assert(_stream != null); | ||
if (loadSync && _stream.CanSeek) | ||
{ | ||
int streamLen = (int)_stream.Length; | ||
|
@@ -339,9 +344,9 @@ public void PlayLooping() | |
LoadAndPlay(Interop.WinMM.SND_LOOP | Interop.WinMM.SND_ASYNC); | ||
} | ||
|
||
private static Uri ResolveUri(string partialUri) | ||
private static Uri? ResolveUri(string partialUri) | ||
{ | ||
Uri result = null; | ||
Uri? result = null; | ||
try | ||
{ | ||
result = new Uri(partialUri); | ||
|
@@ -399,7 +404,7 @@ private void SetupSoundLocation(string soundLocation) | |
} | ||
} | ||
|
||
private void SetupStream(Stream stream) | ||
private void SetupStream(Stream? stream) | ||
{ | ||
if (_copyTask != null) | ||
{ | ||
|
@@ -420,10 +425,10 @@ private void SetupStream(Stream stream) | |
|
||
public void Stop() | ||
{ | ||
Interop.WinMM.PlaySound((byte[])null, IntPtr.Zero, Interop.WinMM.SND_PURGE); | ||
Interop.WinMM.PlaySound((byte[]?)null, IntPtr.Zero, Interop.WinMM.SND_PURGE); | ||
} | ||
|
||
public event AsyncCompletedEventHandler LoadCompleted | ||
public event AsyncCompletedEventHandler? LoadCompleted | ||
{ | ||
add | ||
{ | ||
|
@@ -435,7 +440,7 @@ public event AsyncCompletedEventHandler LoadCompleted | |
} | ||
} | ||
|
||
public event EventHandler SoundLocationChanged | ||
public event EventHandler? SoundLocationChanged | ||
{ | ||
add | ||
{ | ||
|
@@ -447,7 +452,7 @@ public event EventHandler SoundLocationChanged | |
} | ||
} | ||
|
||
public event EventHandler StreamChanged | ||
public event EventHandler? StreamChanged | ||
{ | ||
add | ||
{ | ||
|
@@ -461,22 +466,22 @@ public event EventHandler StreamChanged | |
|
||
protected virtual void OnLoadCompleted(AsyncCompletedEventArgs e) | ||
{ | ||
((AsyncCompletedEventHandler)Events[s_eventLoadCompleted])?.Invoke(this, e); | ||
((AsyncCompletedEventHandler?)Events[s_eventLoadCompleted])?.Invoke(this, e); | ||
} | ||
|
||
protected virtual void OnSoundLocationChanged(EventArgs e) | ||
{ | ||
((EventHandler)Events[s_eventSoundLocationChanged])?.Invoke(this, e); | ||
((EventHandler?)Events[s_eventSoundLocationChanged])?.Invoke(this, e); | ||
} | ||
|
||
protected virtual void OnStreamChanged(EventArgs e) | ||
{ | ||
((EventHandler)Events[s_eventStreamChanged])?.Invoke(this, e); | ||
((EventHandler?)Events[s_eventStreamChanged])?.Invoke(this, e); | ||
} | ||
|
||
private async Task CopyStreamAsync(CancellationToken cancellationToken) | ||
{ | ||
Exception exception = null; | ||
Exception? exception = null; | ||
try | ||
{ | ||
// setup the http stream | ||
|
@@ -485,7 +490,7 @@ private async Task CopyStreamAsync(CancellationToken cancellationToken) | |
#pragma warning disable SYSLIB0014 // WebRequest, HttpWebRequest, ServicePoint, and WebClient are obsolete. Use HttpClient instead. | ||
WebRequest webRequest = WebRequest.Create(_uri); | ||
#pragma warning restore SYSLIB0014 | ||
using (cancellationToken.Register(r => ((WebRequest)r).Abort(), webRequest)) | ||
using (cancellationToken.Register(r => ((WebRequest)r!).Abort(), webRequest)) | ||
{ | ||
WebResponse webResponse = await webRequest.GetResponseAsync().ConfigureAwait(false); | ||
_stream = webResponse.GetResponseStream(); | ||
|
@@ -494,6 +499,7 @@ private async Task CopyStreamAsync(CancellationToken cancellationToken) | |
|
||
_streamData = new byte[BlockSize]; | ||
|
||
Debug.Assert(_stream != null); | ||
int readBytes = await _stream.ReadAsync(_streamData.AsMemory(_currentPos, BlockSize), cancellationToken).ConfigureAwait(false); | ||
int totalBytes = readBytes; | ||
|
||
|
@@ -525,6 +531,7 @@ private async Task CopyStreamAsync(CancellationToken cancellationToken) | |
AsyncCompletedEventArgs ea = exception is OperationCanceledException ? | ||
new AsyncCompletedEventArgs(null, cancelled: true, null) : | ||
new AsyncCompletedEventArgs(exception, cancelled: false, null); | ||
Debug.Assert(_asyncOperation != null); | ||
_asyncOperation.PostOperationCompleted(_loadAsyncOperationCompleted, ea); | ||
} | ||
} | ||
|
@@ -539,7 +546,7 @@ private unsafe void ValidateSoundFile(string fileName) | |
|
||
try | ||
{ | ||
Interop.WinMM.WAVEFORMATEX waveFormat = null; | ||
Interop.WinMM.WAVEFORMATEX? waveFormat = null; | ||
var ckRIFF = new Interop.WinMM.MMCKINFO() | ||
{ | ||
fccType = mmioFOURCC('W', 'A', 'V', 'E') | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.