akram badr

hi all,

i have an application that contain a parent form and i created inside it a multi child forms

how can i create each child form in sperated thread



Re: Windows Forms General create multi child froms in multithread

JRQ

You can't do that.

1. You can create multiple child forms that start a thread.
2. Invoke a method in the main thread/form that creates a child form from a separate thread.





Re: Windows Forms General create multi child froms in multithread

akram badr

ok,

can you show me a demo code about what are you talk





Re: Windows Forms General create multi child froms in multithread

akram badr

i tried to Invoke a method in the main form that creates a child form from a separate thread. and it does not work

can any one help me by a demo code





Re: Windows Forms General create multi child froms in multithread

JRQ

This is a project with Form1 and Form2(child forms)

using System;

using System.Drawing;

using System.Collections;

using System.ComponentModel;

using System.Windows.Forms;

using System.Data;

using System.Threading;

namespace WindowsApplication2

{

/// <summary>

/// Summary description for Form1.

/// </summary>

public class Form1 : System.Windows.Forms.Form

{

private delegate void MethodDelegate();

private System.Threading.Thread thread1;

private System.Windows.Forms.Button button1;

/// <summary>

/// Required designer variable.

/// </summary>

private System.ComponentModel.Container components = null;

public Form1()

{

//

// Required for Windows Form Designer support

//

InitializeComponent();

//

// TODO: Add any constructor code after InitializeComponent call

//

}

/// <summary>

/// Clean up any resources being used.

/// </summary>

protected override void Dispose( bool disposing )

{

if( disposing )

{

if (components != null)

{

components.Dispose();

}

}

base.Dispose( disposing );

}

#region Windows Form Designer generated code

/// <summary>

/// Required method for Designer support - do not modify

/// the contents of this method with the code editor.

/// </summary>

private void InitializeComponent()

{

this.button1 = new System.Windows.Forms.Button();

this.SuspendLayout();

//

// button1

//

this.button1.Location = new System.Drawing.Point(96, 64);

this.button1.Name = "button1";

this.button1.TabIndex = 0;

this.button1.Text = "button1";

this.button1.Click += new System.EventHandler(this.button1_Click);

//

// Form1

//

this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);

this.ClientSize = new System.Drawing.Size(292, 273);

this.Controls.Add(this.button1);

this.Name = "Form1";

this.Text = "Form1";

this.ResumeLayout(false);

}

#endregion

/// <summary>

/// The main entry point for the application.

/// </summary>

[STAThread]

static void Main()

{

Application.Run(new Form1());

}

private void button1_Click(object sender, System.EventArgs e)

{

ThreadStart dlg = new ThreadStart(FormCreator);

thread1 = new Thread(dlg);

thread1.Start();

}

private void CreateChildren()

{

if(this.InvokeRequired) // Invoke self, this will call the same method in the main thread

{

MethodDelegate dlg = new MethodDelegate(CreateChildren);

this.Invoke(dlg);

}

else // if caller is not from a separate thread or was invoked

{

Form2 frm = new Form2();

frm.Show();

}

}

private void FormCreator()

{

for(int i = 0; i < 5; i++)

{

CreateChildren();

}

}

}

}