Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7587779
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T19:45:36+00:00 2026-05-30T19:45:36+00:00

After watching this video , I started thinking about how I could implement something

  • 0

After watching this video, I started thinking about how I could implement something like that in my current project. I think it would be too difficult to make the bulk of my code editable in real-time, but I thought I could at least make my OpenGL shaders editable as I play the game.

So I set up a FileSystemWatcher:

protected void WatchShaders()
{
    _uiDispatcher = Dispatcher.CurrentDispatcher;
    const string shaderDir = @"path\to\my\shaders";
    _shaderFileWatcher = new FileSystemWatcher(shaderDir);
    _shaderFileWatcher.NotifyFilter = NotifyFilters.LastWrite;
    //fw.Filter = "*.frag;*.vert";
    _shaderFileWatcher.Changed += ShaderChanged;
    _shaderFileWatcher.EnableRaisingEvents = true;
}

And now I want to update the shader whenever a file changes:

void ShaderChanged(object sender, FileSystemEventArgs e)
{
    _shaderFileWatcher.EnableRaisingEvents = false; // prevent more/duplicate events from firing before we've finished processing the current one

    lock (_bfShader)
    {
        _bfShader.AttachShader(Shader.FromFile(e.FullPath));
        _bfShader.Link();
        _bfShader.Use();

        _bfProjUniform = new Uniform(_bfShader, "ProjectionMatrix");
        _bfSampler = new Uniform(_bfShader, "TexSampler");
        _bfSampler.Set1(0);
    }
    _shaderFileWatcher.EnableRaisingEvents = true;
}

The problem is as soon as I edit my shader file, an exception is throwing saying:

No context is current in the calling thread

So I did some digging and found out that the OpenGL context is essentially bound to a single thread. AFAIK, there are 2 workarounds for this:

  1. Disable the OpenGL context on the main UI thread, enable it on the other thread, do my stuff, and then reset it
  2. Dispatch the event back to the main UI thread

I’m not sure how I would implement (1) because the main thread is riddled with OpenGL calls… I wouldn’t know where to enable and disable it.

So I’m left with option (2), except I can’t figure out how to dispatch the file changed event back to the main thread.

This article says:

The GLControl provides the GLControl.BeginInvoke() method to simplify asynchronous method calls from secondary threads to the main System.Windows.Forms.Application thread. The GameWindow does not provide a similar API.

Unfortunately I am using a GameWindow, so I’m not sure how to access that functionality.

So what’s the easiest way to dispatch my event back to the main UI thread? Either using the OpenTK library or another preferably non-windows-only library?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T19:45:38+00:00Added an answer on May 30, 2026 at 7:45 pm

    Figured out I could just use a queue and pull things off it:

    void ShaderChanged(object sender, FileSystemEventArgs e)
    {
        _shaderFileWatcher.EnableRaisingEvents = false; // prevent more/duplicate events from firing before we've finished processing the current one
    
        lock (_renderQueue)
        {
            _renderQueue.Enqueue(() =>
                {
                    switch(e.Name)
                    {
                        case "block.frag":
                            _bfShader.DetachShader(_blockFragShader);
                            _blockFragShader = Shader.FromFile(e.FullPath);
                            _bfShader.AttachShader(_blockFragShader);
                            break;
                        default:
                            return;
                    }
    
                    Trace.TraceInformation("Updating shader '{0}'", e.Name);
    
                    _bfShader.Link();
                    _bfShader.Use();
    
                    _bfProjUniform = new Uniform(_bfShader, "ProjectionMatrix");
                    _bfSampler = new Uniform(_bfShader, "TexSampler");
                    _bfSampler.Set1(0);
                });
        }
    
        _shaderFileWatcher.EnableRaisingEvents = true;
    }
    

    And then I modify my render loop slightly:

    lock(_renderQueue)
    {
        while(_renderQueue.Count > 0)
        {
            _renderQueue.Dequeue().Invoke();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Introduction After watching this video from LIDNUG, about .NET code protection http://secureteam.net/lidnug_recording/Untitled.swf (especially from
After watching this video , I am wondering if I am using my controllers
After watching the very known video on this topic I decided to go with
After watching the Knockout.JS video with Steve Sanderson I decided this would be great
After watching Channel 9's video on F# Type Providers, I'm wondering about data schema
We have recently switched to Mercurial. After watching this helpful video: http://www.youtube.com/watch?v=-k2vLKOUb8s&noredirect=1 I am
I'm building my first GAE app. After watching a tutorial video on youtube, (
After watching RailsCasts #273 I want to use the Geocoder gem. I've seen this:
I have a SQL query running using something like this: PreparedStatement pstmt = dbConn.prepareStatement(sqlQuery);
After watching the Pragmatic Studio screen casts on Erlang, the last video on Supervisors

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.