Blazor web assembly: cannot stop img streaming when leaving page

%3CLINGO-SUB%20id%3D%22lingo-sub-3007955%22%20slang%3D%22en-US%22%3EBlazor%20web%20assembly%3A%20cannot%20stop%20img%20streaming%20when%20leaving%20page%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3007955%22%20slang%3D%22en-US%22%3E%3CP%3EHi%2C%3C%2FP%3E%3CP%3EI'm%20having%20a%20problem%20with%20the%20disposing%20of%20a%20Blazor%20Web%20Assembly%20page%20containing%20an%20img%20tag%20having%20as%20src%20the%20path%20of%20a%20camera%20stream.%20In%20short%2C%20when%20I%20leave%20the%20page%2C%20the%20stream%20is%20not%20interrupted%20and%20remains%20active%2C%20wasting%20bandwitdh%20and%20resources.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI've%20the%20following%20Blazor%20page%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-applescript%22%3E%3CCODE%3E%40page%20%22%2Fcounter%22%0A%40implements%20IDisposable%0A%0A%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2F%40imgSrc%22%20%2F%3E%0A%3CBR%20%2F%3E%0A%3CBUTTON%3EHide%2FShow%20Image%3C%2FBUTTON%3E%0A%0A%40code%20%7B%0A%20%20%20%20private%20string%20imgSrc%3B%0A%20%20%20%20private%20string%20imgPath%20%3D%20%22http%3A%2F%2Fuser%3Apassword%40192.168.x.x%2Faxis-cgi%2Fmjpg%2Fvideo.cgi%22%3B%0A%0A%20%20%20%20protected%20override%20async%20void%20OnInitialized()%20%7B%0A%20%20%20%20%20%20%20%20imgsrc%3DimgPath%3B%0A%20%20%20%20%7D%0A%0A%20%20%20%20private%20void%20OnBtnClicked()%20%7B%0A%20%20%20%20%20%20%20%20imgsrc%3D(imgSrc.Equals(%22%22)%20%3F%20imgPath%20%3A%20%22%22)%3B%0A%20%20%20%20%20%20%20%20StateHasChanged()%3B%0A%20%20%20%20%7D%0A%0A%20%20%20%20public%20void%20Dispose()%20%7B%0A%20%20%20%20%20%20%20%20Console.WriteLine(%22Disposing!%22)%3B%0A%20%20%20%20%7D%0A%7D%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EWhen%20I%20enter%20couter%20page%20the%20streaming%20starts%2C%20and%20I%20can%20see%20badwitdh%20increasing%20from%200Mbps%20to%20about%204Mbps%20from%20Task%20Manager%3B%20if%20I%20click%20the%20button%20to%20%22hide%22%20the%20image%2C%20the%20bandwitdh%20goes%20back%20to%200Mbps.%3CBR%20%2F%3EHowever%2C%20if%20while%20the%20live%20stream%20is%20being%20shown%20I%20navigate%20e.g.%20to%20Home%20page%2C%20the%20bandwitdh%20remains%20to%204Mbps.%20At%20this%20point%2C%20even%20if%20I%20go%20back%20to%20the%20couter%20page%20and%20click%20the%20button%20to%20hide%20the%20image%2C%20even%20if%20the%20image%20correctly%20hide%2C%20the%20bandwitdh%20remains%204Mbps.%3C%2FP%3E%3CP%3EIt's%20like%20an%20underlying%20connection%20remains%20active%20and%20Blazor%20has%20no%20means%20to%20stop%20it%2C%20and%20disposing%20the%20page%20does%20not%20stop%20it%20either.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EDo%20you%20know%20if%20there%20is%20a%20clean%20way%20to%20stop%20this%20stream%3F%3C%2FP%3E%3CUL%3E%3CLI%3Eplease%20note%20calling%20OnBtnClicked%20from%20inside%20Dispose()%20yield%20no%20result%2C%20as%20if%20once%20entered%20in%20Dispose()%2C%20user%20interface%20is%20no%20more%20updated%20and%20thus%20the%20instructions%20have%20no%20effect.%3C%2FLI%3E%3CLI%3EThere%20actually%20is%20a%20very%20dirty%20way%20to%20solve%20this%2C%20managing%20manually%20all%20the%20navigation%20related%20stuff%20from%20the%20mainlayout%20page%20and%20performing%20some%20cleaning%20code%20before%20disposing%20the%20page...%20but%20it%20is%20a%20workaround%20and%20not%20a%20very%20nice%20one.%3C%2FLI%3E%3C%2FUL%3E%3CP%3EThank%20you%20very%20much!%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-3007955%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EBlazor%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Occasional Visitor

Hi,

I'm having a problem with the disposing of a Blazor Web Assembly page containing an img tag having as src the path of a camera stream. In short, when I leave the page, the stream is not interrupted and remains active, wasting bandwitdh and resources.

 

I've the following Blazor page:

 

@page "/counter"
@implements IDisposable

<img src="@imgSrc" />
<br />
<button @onclick="OnBtnClicked">Hide/Show Image</button>

@code {
    private string imgSrc;
    private string imgPath = "http://user:password@192.168.x.x/axis-cgi/mjpg/video.cgi";

    protected override async void OnInitialized() {
        imgsrc=imgPath;
    }

    private void OnBtnClicked() {
        imgsrc=(imgSrc.Equals("") ? imgPath : "");
        StateHasChanged();
    }

    public void Dispose() {
        Console.WriteLine("Disposing!");
    }
}

 

 

When I enter counter page the streaming starts, and I can see badwitdh increasing from 0Mbps to about 4Mbps from Task Manager; if I click the button to "hide" the image, the bandwitdh goes back to 0Mbps.
However, if while the live stream is being shown I navigate e.g. to Home page, the bandwitdh remains to 4Mbps. At this point, even if I go back to the counter page and click the button to hide the image, even if the image correctly hides, the bandwitdh remains 4Mbps.

It's like an underlying connection remains active and Blazor has no means to stop it, and disposing the page does not stop it either.

 

Do you know if there is a clean way to stop this stream?

  • please note calling OnBtnClicked from inside Dispose() yield no result, as if once entered in Dispose(), user interface is no more updated and thus the instructions have no effect.
  • There actually is a very dirty way to solve this, managing manually all the navigation related stuff from the main layout page and performing some cleaning before disposing the page... but the use of this workaround force me to setup a complex structure just to fix this problem.

Thank you very much!

0 Replies