开发者

Keep thread waiting on windows form response?

开发者 https://www.devze.com 2023-01-09 11:02 出处:网络
I have a windows forms application that runs two threads simultaneously, with the UI thread running in the task bar. The UI thread actually performs completely separate functionality from the other pr

I have a windows forms application that runs two threads simultaneously, with the UI thread running in the task bar. The UI thread actually performs completely separate functionality from the other process, but in the case that a new user logs into the application, I need to pop up a setup window from the non-UI thread.

Here is the code from my Program.cs:

static void Main()
    {
        ThreadStart start = new ThreadStart(Waiting.wait);
        Thread waiting = new Thread(start);
        waiting.Start();
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(true);
        Application.Run(TaskBarIcon.getInstance());
    }

Currently, before TaskBarIcon can fully instantiate, one configuration method must be finished running in the waiting thread. This is achieved 开发者_StackOverflowby passing a lock back and forth. I would like to have this set up menu pop up while the configuration method is processing, and have the method wait to complete until the setup menu is done running. However, unless I run the set up menu directly from the Application.Run() method, I cannot even get the menu to show up properly.

I'm very new to C#....would be able to do this quickly in Java, but C# seems to do things differently.

Any suggestions or solutions would be greatly appreciated!

badPanda


I suspect what it is happening is that the setup menu form is not receiving windows messages on the worker thread and that is why it is not showing up. All forms and controls need a message pump to work properly. There are various different ways of getting a message pump started, but the two most applicable to you are:

  • Application.Run
  • Form.ShowDialog

If you call ShowDialog on the setup menu form then it should show up on the worker thread. Of course the call blocks until the form is closed so that will prevent the remainder of the configuration method from executing, but then again that may be exactly what you want.

If you want the main UI thread (the one calling Application.Run) to wait until this configuration method is finished then use a WaitHandle to signal when the configuration task is complete. It might look like the following.

static void Main() 
{ 
  var configured = new ManualResetEvent(false);
  var worker = new Thread(
    () =>
    {
       // Do some stuff here.

       CallYourConfigurationMethod();
       configured.Set() // Signal that configuration is complete.

       // Do some more stuff here.
    });
  worker.Start(); 
  configured.WaitOne();
  Application.EnableVisualStyles(); 
  Application.SetCompatibleTextRenderingDefault(true); 
  Application.Run(TaskBarIcon.getInstance()); 
} 
0

精彩评论

暂无评论...
验证码 换一张
取 消