public static void VW(object param) { VideoStreamForm vsf = (VideoStreamForm)param; try { lock (vsf.eventConsumer) { vsf.streamUri = vsf.getStreamUri(vsf.streamSetup, vsf.profile.token); } } catch (Exception ex) { vsf.exception = ex; } if (vsf.exception == null) { IVideoForm videoForm = new VideoContainer(); videoForm.NICIndex = vsf.NICIndex; VideoUtils.AdjustVideo( videoForm, vsf.username, vsf.password, vsf.messageTimeout, vsf.streamSetup.Transport.Protocol, vsf.streamSetup.Stream, vsf.streamUri, vsf.profile.VideoEncoderConfiguration); bool VideoIsOpened = false; try { VideoIsOpened = true; videoForm.EventSink = vsf; videoForm.OpenWindow(false); ((AutoResetEvent)vsf.eventOpened).Set(); vsf.signalCloseWindow.WaitOne(); videoForm.CloseWindow(); VideoIsOpened = false; } catch (Exception ex) { vsf.exception = ex; } finally { if (VideoIsOpened) { videoForm.CloseWindow(); } videoForm = null; } } ((ManualResetEvent)vsf.eventWorkEnded).Set(); ((AutoResetEvent)vsf.signalCloseWindow).Set(); }
public static void ShowMultiple( IVideoFormEvent eventConsumer, WaitHandle stopEvent, string username, string password, int timeout, GetStreamSetup getStreamSetup, int NICIndex, List <Profile> profiles, Func <StreamSetup, string, MediaUri> getStreamUri, Action <Action, string, bool> runStep, Action <Exception> stepFailed) { List <VideoStreamForm> streams = new List <VideoStreamForm>(); List <WaitHandle> eventsWorkEnded = new List <WaitHandle>(); timeout = Math.Max(timeout, 2000 * profiles.Count()); WaitHandle signalCloseWindow = new AutoResetEvent(false); foreach (Profile profile in profiles) { Profile currentProfile = profile; StreamSetup streamSetup = getStreamSetup(ref currentProfile); if (currentProfile == null) { continue; } VideoStreamForm vsf = new VideoStreamForm(); vsf.streamSetup = streamSetup; vsf.profile = currentProfile; vsf.getStreamUri = getStreamUri; vsf.messageTimeout = timeout; vsf.username = username; vsf.password = password; vsf.signalCloseWindow = signalCloseWindow; vsf.eventConsumer = eventConsumer; vsf.NICIndex = NICIndex; streams.Add(vsf); eventsWorkEnded.Add(vsf.eventWorkEnded); } if (0 == streams.Count) { return; } bool timeoutException = false; try { VideoStreamForm vsfFailed = null; foreach (VideoStreamForm vsf in streams) { ((ManualResetEvent)vsf.eventWorkEnded).Reset(); vsf.thread.Start(vsf); if (0 == WaitHandle.WaitAny(new WaitHandle[] { stopEvent, vsf.eventOpened, vsf.eventWorkEnded })) { throw new StopEventException(); } if (vsf.exception != null) { stepFailed(vsf.exception); vsfFailed = vsf; break; } } foreach (VideoStreamForm vsf in streams) { if (vsf.thread.ThreadState != ThreadState.Unstarted && vsf != vsfFailed) { vsf.skipLog = true; } } ((AutoResetEvent)signalCloseWindow).Set(); runStep(() => { if (!WaitHandle.WaitAll(eventsWorkEnded.ToArray(), timeout)) { ((AutoResetEvent)signalCloseWindow).Reset(); timeoutException = true; throw new VideoException("Waiting timeout exceeded"); } }, "Closing streams", false); if (timeoutException) { throw new VideoException("Waiting timeout exceeded"); } } // handle halt catch (Exception ex) { runStep(() => { foreach (VideoStreamForm vsf in streams) { if (vsf.thread.ThreadState != ThreadState.Unstarted) { vsf.thread.Abort(); vsf.thread.Join(); } } }, "Cleanup", true); if (!timeoutException) { throw ex; } } runStep(() => { // check for exceptions if (timeoutException) { throw new VideoException("Closing streams failed!"); } foreach (VideoStreamForm vsf in streams) { vsf.thread.Join(); if (vsf.exception != null) { throw new VideoException("Profile " + vsf.profile.Name + " failed!"); } } }, "Check for test results", true); }