Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Cross-thread events

Cross-thread events

Scheduled Pinned Locked Moved C#
winformsdesignhelptutorialquestion
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • 1 Offline
    1 Offline
    1nsp1r3d
    wrote on last edited by
    #1

    Hi all, Let me introduce you to the problem which I'm trying to cope with. I'm having two windows forms in 2 threads - one is a splash screen and the other one is the main window. On starting, the splash is visible and the main is hidden. I've written everything to the point where I've to implement signalling the splash that main is ready and closing the splash. What I've tried yesterday is to implement this with timers, so splash has a 100ms timer which checks a property on the mainwindow class and if it's true, then the splash closes itself. But then, how would I synchronise the closing of the splash with setting the main form visibility to true so they happen syncroniously(one after the other). What I've been thinking about is using events, but I'm not sure how to do this in cross-threads windows forms, ie how to design it. Should I have event for the main which is emitted when it's ready and the splash listens for this event? And then it closes itself and emits another signal that's has closed and the main listens for the closing signal of the splash? If so, how would I attach to listening to the signals across the threads? In my Main() function, I create both threads and I've two properties of the EntryPoint class(which contains the main and nothing else) - the splash form and the main form. And I have two methods which are used for the entry point of the the both, which are basically doing: Application.Run(this.splashform); Any advice is greatly appreciated. Thanks for reading.

    J 1 Reply Last reply
    0
    • 1 1nsp1r3d

      Hi all, Let me introduce you to the problem which I'm trying to cope with. I'm having two windows forms in 2 threads - one is a splash screen and the other one is the main window. On starting, the splash is visible and the main is hidden. I've written everything to the point where I've to implement signalling the splash that main is ready and closing the splash. What I've tried yesterday is to implement this with timers, so splash has a 100ms timer which checks a property on the mainwindow class and if it's true, then the splash closes itself. But then, how would I synchronise the closing of the splash with setting the main form visibility to true so they happen syncroniously(one after the other). What I've been thinking about is using events, but I'm not sure how to do this in cross-threads windows forms, ie how to design it. Should I have event for the main which is emitted when it's ready and the splash listens for this event? And then it closes itself and emits another signal that's has closed and the main listens for the closing signal of the splash? If so, how would I attach to listening to the signals across the threads? In my Main() function, I create both threads and I've two properties of the EntryPoint class(which contains the main and nothing else) - the splash form and the main form. And I have two methods which are used for the entry point of the the both, which are basically doing: Application.Run(this.splashform); Any advice is greatly appreciated. Thanks for reading.

      J Offline
      J Offline
      Judah Gabriel Himango
      wrote on last edited by
      #2

      Why use 2 threads? You're introducing complexity unnecessarily. In the constructor for your main window, I assume you have the InitializeComponent() call that actually initializes your main window. Change that constructor to

      public MainWindow()
      {
      // Show the splash screen.
      SplashWindow splash = new SplashWindow();
      splash.Show(); // Use Show() instead of ShowDialog, as ShowDialog is blocking, whereas Show() returns immediately.

      // Now that we've shown the splash, start loading our main window.
      InitializeComponent();
      

      }

      And when your MainWindow finishes loading (override the OnLoad method or create an OnLoad event handler), you can set the visibility of both forms synchronously.

      Tech, life, family, faith: Give me a visit. I'm currently blogging about: Conversation With a Muslim Judah Himango

      1 1 Reply Last reply
      0
      • J Judah Gabriel Himango

        Why use 2 threads? You're introducing complexity unnecessarily. In the constructor for your main window, I assume you have the InitializeComponent() call that actually initializes your main window. Change that constructor to

        public MainWindow()
        {
        // Show the splash screen.
        SplashWindow splash = new SplashWindow();
        splash.Show(); // Use Show() instead of ShowDialog, as ShowDialog is blocking, whereas Show() returns immediately.

        // Now that we've shown the splash, start loading our main window.
        InitializeComponent();
        

        }

        And when your MainWindow finishes loading (override the OnLoad method or create an OnLoad event handler), you can set the visibility of both forms synchronously.

        Tech, life, family, faith: Give me a visit. I'm currently blogging about: Conversation With a Muslim Judah Himango

        1 Offline
        1 Offline
        1nsp1r3d
        wrote on last edited by
        #3

        Wouldn't the splash be blocked by the main form and not receive events? Because that's what I think will happen and most solutions for a splash use threads. Or I could be wrong...

        J 1 Reply Last reply
        0
        • 1 1nsp1r3d

          Wouldn't the splash be blocked by the main form and not receive events? Because that's what I think will happen and most solutions for a splash use threads. Or I could be wrong...

          J Offline
          J Offline
          Judah Gabriel Himango
          wrote on last edited by
          #4

          1nsp1r3d wrote: Wouldn't the splash be blocked by the main form and not receive events? No, the splash will be free to receive events. Using multiple threads to touch different UI components is a widely-known no-no anyways. If you update your splash screen, you may need to call .Update() to force a redraw, however, since the OS is busy loading your main form.

          Tech, life, family, faith: Give me a visit. I'm currently blogging about: Homosexuals for Christ, Yeah! Judah Himango

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups