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. Problem in opening model dialog window from worker thread

Problem in opening model dialog window from worker thread

Scheduled Pinned Locked Moved C#
help
6 Posts 4 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.
  • K Offline
    K Offline
    Krrish
    wrote on last edited by
    #1

    Can any one help me out, I am trying to run a seperate thread from my main thread to perform some functionality, my problem is I am trying to open a modal dialog from my child thread, even I use Form.ShowDialog() method this is behaving as modeless and it is allowing access to other screens in the mdi which should be restriceted. Any help would be appreciated Regards, Krishna

    L S L 3 Replies Last reply
    0
    • K Krrish

      Can any one help me out, I am trying to run a seperate thread from my main thread to perform some functionality, my problem is I am trying to open a modal dialog from my child thread, even I use Form.ShowDialog() method this is behaving as modeless and it is allowing access to other screens in the mdi which should be restriceted. Any help would be appreciated Regards, Krishna

      L Offline
      L Offline
      leppie
      wrote on last edited by
      #2

      You should not be creating controls on other threads. Use Control.Invoke() from you worker thread.**

      xacc.ide-0.1.3.14 - Now with syntax support for PowerShell
      xacc.ide-0.1.3.13 source code

      **

      1 Reply Last reply
      0
      • K Krrish

        Can any one help me out, I am trying to run a seperate thread from my main thread to perform some functionality, my problem is I am trying to open a modal dialog from my child thread, even I use Form.ShowDialog() method this is behaving as modeless and it is allowing access to other screens in the mdi which should be restriceted. Any help would be appreciated Regards, Krishna

        S Offline
        S Offline
        S Senthil Kumar
        wrote on last edited by
        #3

        To substantiate Leppie's post, when you call ShowDialog on a thread other than the UI thread, it essentially behaves as a modeless dialog. To do what you want, you have to make the call come from the UI thread, which you can do by calling Invoke[^] on the main form or any of the main form's controls. This will cause the delegate to be called from the UI thread and your form will be modal. Regards Senthil _____________________________ My Blog | My Articles | WinMacro

        1 Reply Last reply
        0
        • K Krrish

          Can any one help me out, I am trying to run a seperate thread from my main thread to perform some functionality, my problem is I am trying to open a modal dialog from my child thread, even I use Form.ShowDialog() method this is behaving as modeless and it is allowing access to other screens in the mdi which should be restriceted. Any help would be appreciated Regards, Krishna

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          e.g you have a method called ShowMyDialog(). You cannot call this method from another thread. Then you must use this code, if you want to call ShowMyDialog() from another thread :

          MethodInvoker mInvoker = new MethodInvoker(ShowMyDialog);
          Invoke(mInvoker);

          K 1 Reply Last reply
          0
          • L Lost User

            e.g you have a method called ShowMyDialog(). You cannot call this method from another thread. Then you must use this code, if you want to call ShowMyDialog() from another thread :

            MethodInvoker mInvoker = new MethodInvoker(ShowMyDialog);
            Invoke(mInvoker);

            K Offline
            K Offline
            Krrish
            wrote on last edited by
            #5

            Can ypu pls explain it with a simple example Krishna

            L 1 Reply Last reply
            0
            • K Krrish

              Can ypu pls explain it with a simple example Krishna

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              using System;
              using System.Drawing;
              using System.Collections;
              using System.ComponentModel;
              using System.Windows.Forms;
              using System.Data;
              using System.Threading;

              namespace WindowsApplication5
              {
              public class Form1 : System.Windows.Forms.Form
              {
              private System.Windows.Forms.Button button1;
              private System.ComponentModel.Container components = null;

              	public Form1()
              	{	InitializeComponent(); }
              
              	protected override void Dispose( bool disposing )
              	{
              		if( disposing )
              		{
              			if (components != null) 
              			{ components.Dispose(); }
              		}
              		base.Dispose( disposing );
              	}
              
              	private void InitializeComponent()
              	{
              		this.button1 = new System.Windows.Forms.Button();
              		this.SuspendLayout();
              		this.button1.Location = new System.Drawing.Point(48, 72);
              		this.button1.Name = "button1";
              		this.button1.TabIndex = 1;
              		this.button1.Text = "Change text";
              		this.button1.Click += new System.EventHandler(this.button1\_Click);
              		this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
              		this.ClientSize = new System.Drawing.Size(208, 189);
              		this.Controls.Add(this.button1);
              		this.Name = "Form1";
              		this.Text = "Form1";
              		this.ResumeLayout(false);
              	}
              
              	\[STAThread\]
              	static void Main() 
              	{	Application.Run(new Form1()); }
              
              	private void button1\_Click(object sender, System.EventArgs e)
              	{
              		/\*\*\*\*\* WRONG \*\*\*\*\*/
              

              // Thread t = new Thread(new ThreadStart(Run));
              // t.Start();

              		/\*\*\*\*\* OK \*\*\*\*\*\*/
              		Thread t = new Thread(new ThreadStart(CreateNewLabel));
              		t.Start();
              	}
              
              	private void CreateNewLabel()
              	{
              		MethodInvoker minvoker = new MethodInvoker(Run);
              		Invoke(minvoker);
              	}
              
              	private void Run()
              	{
              		try
              		{
              			Label label = new Label();
              			label.Name = "MyLabel";
              			label.Text = "MyLabel";
              			label.Location = new Point(50, 50);
              			this.Controls.Add(label);
              		}
              		catch(Exception ex)
              		{	MessageBox.Show(ex.ToString());	}
              	}
              }
              

              }

              You cannot call method "Run" directly, because method "Run" is in another thread. That's why you must call the method CreateNewLabel to create a new label from another thread. You can find the complete description in : http://www.oreilly.de/catalog/csharpnutger/chapter/ch04.html That site is in german, you can use www.google.com/translate to translate it in english.

              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