Results 1 to 6 of 6

Thread: Problem with SynchronizationContext.Current

  1. #1

    Thread Starter
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Problem with SynchronizationContext.Current

    I'm using the Pauser posted by jmc to manage my game time.
    Within this code the SynchronizationContext.Current is put into a variable.
    I'm wondering why I am getting different result for that SynchronizationContext.Current in circumstances which I believe to be the same.

    The situation:
    The GUI opens a Threadning.Threat in which the GameLoop is run, in this GameLoop the the pauser code is used which is putting the SynchronizationContext.Current into a variable. Since it is not the GUI thread SynchronizationContext.Current IMHO should return Nothing
    The GameLoop is beeing used for direct simulation (it's working as expected there, SynchronizationContext.Current return Nothing) and for Replay(It is not working here, SynchronizationContext.Current returns context = {System.Windows.Forms.WindowsFormsSynchronizationContext}).
    In both cases the GameLoo just calls another Sub in which either a SimulationStep or ReplayStep is done.

    Where is my mistake?
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Problem with SynchronizationContext.Current

    Quote Originally Posted by opus View Post
    Since it is not the GUI thread SynchronizationContext.Current IMHO should return Nothing
    Then your honest opinion is wrong. SynchronizationContext.Current will never be Nothing in a Windows Forms or WPF application. Every thread has a SynchronizationContext object associated with it.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109

    Re: Problem with SynchronizationContext.Current

    Quote Originally Posted by opus View Post
    the GameLoo
    I gotta get me one of those
    My usual boring signature: Nothing

  4. #4

    Thread Starter
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: Problem with SynchronizationContext.Current

    Quote Originally Posted by jmcilhinney View Post
    Then your honest opinion is wrong. SynchronizationContext.Current will never be Nothing in a Windows Forms or WPF application. Every thread has a SynchronizationContext object associated with it.
    Maybe my explanation wasn't clear enough, my GameLoop is run on a seperate Thread NOT on the GUI thread.

    My observations are:
    When used on the GUI thread a SynchronizationContext is returned.
    When used on a seperated "nomally" nothing is returned (except in the case posted above).

    The code posted by jmcilhinney to handle that is working properly

    info.context has been set by SynchronizationContext.Current
    Code:
            If info.context Is Nothing Then
                'This is not a Windows GUI app so execute the method on the current thread.
                Execute(info)
            Else
                'This is a Windows GUI app so execute the method on the original thread.
                info.context.Post(AddressOf Execute, info)
            End If
    Is there a way to check in Debug-Mode wether I'm on the GUI-thread or not? In the moment I'm checking the Thread.Name, according to that it's not the GUI.
    Although I'm getting SynchronizationContext, the .Post doesn't send the code to the Thread where the GameLoop is.
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Problem with SynchronizationContext.Current

    Quote Originally Posted by jmcilhinney View Post
    Then your honest opinion is wrong. SynchronizationContext.Current will never be Nothing in a Windows Forms or WPF application. Every thread has a SynchronizationContext object associated with it.
    Hmmm... looks like I may have been wrong about that. I could have sworn that I'd tested that previously but perhaps not. Anyway, I'll have a closer look at this when I have time, which is not now I'm afraid.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: Problem with SynchronizationContext.Current

    Take your time, it Friday in here as well (my wife already looking suspicious).

    BTW: This is Lale, isn't she cute as knut?
    Name:  lale.jpg
Views: 401
Size:  29.1 KB
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width