Problem with stopping stream in backgroundworker in 0.9.9.7

Oct 7, 2015 at 9:24 AM
Edited Oct 7, 2015 at 9:28 AM
Hi,

I put a user stream into a backgroundworker thread. That ran very smooth so far. Yesterday I updated to 0.9.9.7 and now I get an exception every time I try to stop the stream in the backgroundworker.

DoWork method of my BGWorker
private void watchtask_DoWork(object sender, DoWorkEventArgs e)
        {
            Helper.WriteLog("STREAM TASK - " + myConfig.TaskName + " Entering DoWork");
            DoWork();
        }
Declaring all events for the stream.
private void DoWork()
{
    Auth.ExecuteOperationWithCredentials(creds, () =>
    {
        stream = Stream.CreateUserStream();                
        
        stream.FollowedByUser += (sender, args) =>
        {
           
        };                

        stream.TweetFavouritedByAnyone += (sender, args) =>
        {
            
        };

        stream.TweetCreatedByAnyone += (sender, args) =>
        {
            
        };

        stream.TweetCreatedByFriend += (sender, args) =>
        {
           
        };

        stream.MessageReceived += (sender, args) =>
        {
            
        };

        stream.DisconnectMessageReceived += (sender, args) =>
        {                  
            
        };

        stream.StreamStopped += (sender, args) =>
        {
            
            
        };                
        
        stream.StartStreamAsync();
        
        while (!bgWorker.CancellationPending)
        {
            if (bgWorker.CancellationPending)
            {
                //Cancel = true;
                return;
            }                   

            bgWorker.ReportProgress(10);
            System.Threading.Thread.Sleep(10);
        }
    });
}
Stopping the stream.
private void watchtask_WorkCompleted(object sender, RunWorkerCompletedEventArgs e)
        {                
            // TODO: Following statement throws an target invocation exception starting with 0.9.9.7!
            stream.StopStream();            
            OnTaskDoneEvent("Stream done", 100);
            
        }
Any tips appreciated ;-)
Kay
Coordinator
Oct 7, 2015 at 9:49 AM
Hello,

Thanks for reporting the issue. I will need more information in order to help you.

Following statement throws an target invocation exception

Would you please share the full stack trace as well as any additional information related with the Exception?

starting with 0.9.9.7!

Do you mean that you did not have this issue before? If yes, which version were you using before?

Cheers,
Linvi
Oct 7, 2015 at 10:06 AM
Edited Oct 7, 2015 at 10:32 AM
Hi Linvi,

yes, the error did not occure prior to 0.9.9.7. I was using 0.9.9.6 so far. I worked till yesterday with 0.9.9.6 and everything was fine. Then I saw the update, I updated via NuGet to 0.9.9.7 and without changing any code on my side, the error occurs.

Is this what you need?

The referred "StreamTask" is the BGWorker.

Is there any possibility to downgrade to 0.9.9.6 via NuGet so far to be able to continue my work without having the error?

Edit: In the WorkCompleted method of the BGWorker, the object "stream" is not null by itself, but throws a "nullReferenceException":
"System.NullReferenceException" wurde aufgefangen.
  _HResult=-2147467261
  _message=Der Objektverweis wurde nicht auf eine Objektinstanz festgelegt.
  HResult=-2147467261
  IsTransient=false
  Message=Der Objektverweis wurde nicht auf eine Objektinstanz festgelegt.
  Source=WORKNAME
  StackTrace:
       bei WORKNAME.WatchTask.<DoWork>b__7(Object sender, StreamExceptionEventArgs args) in j:\_DEV_CHECKOUT\WORKNAME\WORKNAME\StreamTask.cs:Zeile 238.
       bei Tweetinvi.Core.Events.EventHelpers.Raise[T](Object o, EventHandler`1 handler, T arg)
       bei Tweetinvi.Streams.Helpers.StreamResultGenerator.StopStream()
       bei Tweetinvi.Streams.TwitterStream.StopStream()
       bei WORKNAME.WatchTask.watchtask_WorkCompleted(Object sender, RunWorkerCompletedEventArgs e) in j:\_DEV_CHECKOUT\WORKNAME\WORKNAME\StreamTask.cs:Zeile 268.
  InnerException: 
This is the exception message from the main thread.
System.Reflection.TargetInvocationException wurde nicht behandelt.
  _HResult=-2146232828
  _message=Ein Aufrufziel hat einen Ausnahmefehler verursacht.
  HResult=-2146232828
  IsTransient=false
  Message=Ein Aufrufziel hat einen Ausnahmefehler verursacht.
  Source=mscorlib
  StackTrace:
       bei System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
       bei System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
       bei System.Delegate.DynamicInvokeImpl(Object[] args)
       bei System.Windows.Forms.Control.InvokeMarshaledCallbackDo(ThreadMethodEntry tme)
       bei System.Windows.Forms.Control.InvokeMarshaledCallbackHelper(Object obj)
       bei System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       bei System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       bei System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       bei System.Windows.Forms.Control.InvokeMarshaledCallback(ThreadMethodEntry tme)
       bei System.Windows.Forms.Control.InvokeMarshaledCallbacks()
       bei System.Windows.Forms.Control.WndProc(Message& m)
       bei System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       bei System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       bei System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       bei System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       bei System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
       bei System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       bei System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       bei System.Windows.Forms.Application.Run(Form mainForm)
       bei WORKNAME.Program.Main() in j:\_DEV_CHECKOUT\WORKNAME\WORKNAME\Program.cs:Zeile 19.
       bei System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       bei System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       bei Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       bei System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       bei System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       bei System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       bei System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       bei System.Threading.ThreadHelper.ThreadStart()
  InnerException: System.NullReferenceException
       _HResult=-2147467261
       _message=Der Objektverweis wurde nicht auf eine Objektinstanz festgelegt.
       HResult=-2147467261
       IsTransient=false
       Message=Der Objektverweis wurde nicht auf eine Objektinstanz festgelegt.
       Source=WORKNAME
       StackTrace:
            bei WORKNAME.WatchTask.<DoWork>b__7(Object sender, StreamExceptionEventArgs args) in j:\_DEV_CHECKOUT\WORKNAME\WORKNAME\StreamTask.cs:Zeile 238.
            bei Tweetinvi.Core.Events.EventHelpers.Raise[T](Object o, EventHandler`1 handler, T arg)
            bei Tweetinvi.Streams.Helpers.StreamResultGenerator.StopStream()
            bei Tweetinvi.Streams.TwitterStream.StopStream()
            bei WORKNAME.WatchTask.watchtask_WorkCompleted(Object sender, RunWorkerCompletedEventArgs e) in j:\_DEV_CHECKOUT\WORKNAME\WORKNAME\StreamTask.cs:Zeile 266.
            bei System.ComponentModel.BackgroundWorker.OnRunWorkerCompleted(RunWorkerCompletedEventArgs e)
            bei System.ComponentModel.BackgroundWorker.AsyncOperationCompleted(Object arg)
       InnerException: 
Oct 7, 2015 at 10:28 AM
Edited Oct 7, 2015 at 10:30 AM
If I write the WorkCompleted method like this:
private void watchtask_WorkCompleted(object sender, RunWorkerCompletedEventArgs e)
        {      
            try
            {
                stream.StopStream();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
            OnTaskDoneEvent("Stream done", 100);
            
        }
The error still occurs BUT the stream is correctly stopped after the exception and I am able to restart the stream again normally.
Coordinator
Oct 7, 2015 at 3:15 PM
Hello,

Would you please try using version 0.9.10.0 of Tweetinvi which is currently available on Github.
Let me know if you encounter the same issue as multi-threaded streams have been completely reworked.

Cheers,
Linvi
Oct 7, 2015 at 5:02 PM
Edited Oct 7, 2015 at 5:02 PM
Thanks for your great support!

I compiled the 0.9.10.0 version but how do I use the new version? Because for now I use the NuGet Package in my project solution which is quite comfortable because of the others dependencies within your library.
Coordinator
Oct 7, 2015 at 5:06 PM
Let me get back to you with a .zip containing what you need.
Oct 7, 2015 at 5:16 PM
No thanks, I made a copy of my solution and changed the references.

I compiled with your 0.9.10.0 version but the error is still the same. As it seems my own code can not be the cause because it's the
stream.StopStream();
method that throws the exception and the stream itself is only configured with the credentials on my side. And in general the stream works, but stopping the stream gives the error.

If you like I can send you the code for my background worker class via mail.
Coordinator
Oct 7, 2015 at 5:20 PM
Edited Oct 7, 2015 at 5:24 PM
Thanks, this would be very useful. You can contact me with private message using my profile screen (https://www.codeplex.com/site/users/view/linvi).
I will reply to you promptly.

Also if you need it, I already uploaded the .zip before you sent the reply (Download Tweetinvi 0.9.10.0).

Cheers,
Linvi
Oct 8, 2015 at 12:52 AM
Problem solved with the superior mega support of linvi.

Thanks again for finding my mistake ;-)

Kay
Marked as answer by kom2009 on 10/7/2015 at 4:52 PM
Coordinator
Oct 8, 2015 at 9:44 AM
Hi,

I am happy that this issue has been fixed but I will add new improvements in version 0.9.10.0 regarding Streams and multi threading.
I have created the following Work Item based on your issue and my investigation of the code.

Feel free to let me know what you think about this.

Cheers,
Linvi