Hi, I’m trying to learn about channels in c# and I’m attempting to build a concurrent webcrawler to do so. So I have an unbounded channel linksToCrawl. when I consume a message (a url), I check if there are desirable links in the dom the url loads and publish those to the channel. apologies if this is a silly question


I want to complete the linksToCrawl channel when it’s empty. I looked into the TryRead method, and thought about implementing something like this
But I’m not certain that TryRead will always return true unless the channel is empty. Any suggestions?
According to this example and docs here
reader.WaitToReadAsync() “returns false if the channel is completed” and ” no further data will ever be available to be read”
it’s “completed” when the writer marks it completed by calling writer.Complete()
The question “is this channel complete?” is a slightly different one to “is this channel empty?” It could be empty because the reader is working faster than the writer, and more items are coming soon, if you wait. It’s “complete” when the writer says it’s done writing.
This is a subtle point; so I think this what happens when you do:
var moreData = await reader.WaitToReadAsync();
If the channel contains data, moreData will be true.
If the channel is empty and has been completed, moreData will be false.
If the channel is temporarily empty, then … WaitToReadAsync does not return immediately! You wait until one of above two condition is met. This is how you suspend the reader until the writer gives it something to do.
C# devs
null reference exceptions

source