dcsimg
Results 1 to 5 of 5

Thread: VB.NET and WPF SplashScreen with multithreading

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Dec 2016
    Posts
    78

    VB.NET and WPF SplashScreen with multithreading

    Following code is C# and WinForm and following code is okey.

    Following code is taken from this youtube video: https://www.youtube.com/watch?v=wZ4BkPyZllY


    Code:
    namespace SplashScreen
    {
    public partial class frmMain : Form
    {
    public frmMain()
    {
        Thread t = new Thread(new ThreadStart(StartForm));
        t.Start();
        Thread.Sleep(5000);
        InitializeComponent();
        t.Abort();
    }
    
    public void StartForm()
    {
        Application.Run(new frmSplashScreen());
    }
    }
    }

    Following code is VB.NET and WinForm and following code is okey.

    Code:
    Class MainWindow 
    Private Sub Button1_Click(sender As Object, e As RoutedEventArgs) Handles Button1.Click
        Dim t As System.Threading.Thread = New System.Threading.Thread(AddressOf StartForm)
        t.Start()
        System.Threading.Thread.Sleep(5000)
        t.Abort()
    End Sub
    Public Sub StartForm()
        Application.Run(New frmSplashScreen)
    End Sub
    End Class
    I want to convert above code to VB.NET and WPF application.

    Following code needs to be repaired.


    Code:
    Class MainWindow 
    Private Sub Button1_Click(sender As Object, e As RoutedEventArgs) Handles Button1.Click
        Dim t As System.Threading.Thread = New System.Threading.Thread(AddressOf StartForm)
        t.Start()
        System.Threading.Thread.Sleep(5000)
        t.Abort()
    End Sub
    Public Sub StartForm()
        Dim mySplashScreenWindow As New SplashScreenWindow()
        mySplashScreenWindow.Owner = Me
        mySplashScreenWindow.ShowDialog()
    End Sub
    End Class
    Here is the error message: The calling thread must be STA, because many UI components require this.

    Here is the error screen shot: https://prnt.sc/kumom7
    Last edited by Kram Kramer; Sep 15th, 2018 at 05:10 AM.

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    100,599

    Re: VB.NET and WPF SplashScreen with multithreading

    Why exactly are you doing this? Is it because your main window takes some time to initialise and you want to display something in the mean time or is it because you specifically want to display some information to the user before the main window? If it's the former then using multi-threading is worthwhile because then the main window can still perform its initialisation on the UI thread. If it's the latter then multi-threading is pointless.

    I suspect that this issue is due to the fact that you are assigning a window created on one thread to the Owner of a window created on another thread. Accessing controls from threads that didn't create them is a no-no and if you have two forms related like that then obviously at least one has to access the other. What's the point of setting the Owner anyway? What exactly do you think that that is doing for you? It's pointless if you call ShowDialog rather than Show.
    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

    Thread Starter
    Lively Member
    Join Date
    Dec 2016
    Posts
    78

    Re: VB.NET and WPF SplashScreen with multithreading

    Hi jmcilhinney,

    I agree that mySplashScreenWindow.Owner = Me is meanless

    I want same thing what youtube video has done.

    The difference is mine is vb.net - wpf application.

    So, do you know how to convert that youtube code to vb.net - wpf code?

    I want to use ShowDialog() not Show() as youtube video has done.
    Last edited by Kram Kramer; Sep 15th, 2018 at 08:29 AM.

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Dec 2016
    Posts
    78

    Re: VB.NET and WPF SplashScreen with multithreading

    Duplicate post.
    Last edited by Kram Kramer; Sep 15th, 2018 at 08:41 AM.

  5. #5
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    100,599

    Re: VB.NET and WPF SplashScreen with multithreading

    Quote Originally Posted by Kram Kramer View Post
    I agree that mySplashScreenWindow.Owner = Me is meanless
    So why is it there then?
    Quote Originally Posted by Kram Kramer View Post
    I want to use ShowDialog() not Show() as youtube video has done.
    You already are doing that I didn't tell you not to so what's the relevance of that statement?

    Have removed the line that sets the Owner and tried again? If not, why not? If so, why haven't you told us what happened?
    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

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width