0
Answer

interacting with two form in a multi-threading pattern

yaser keneshloo

yaser keneshloo

16y
4.7k
1

Hi friends.

I’ve a question. How can take this functionality?

I had two forms in a windows form application(c#): manager form and managernotifycost form.

I want to show both form in following pattern:

1)first form(manager) call Show() method of an second form’s object.

2)second form retrieve data from database and show information in labels, simultaneously first form do something like managing data.

The problem is that when the main thread call MNC.Show() and second form’s thread are started, the main thread proceeded from MNC.Show() while second form’s thread still working, and the second form go to the Not-Responding state and nothing displayed. Note that first form work correctly.

How can i interact with this issue?

If you give me an example that show me how does information display in second form, i finish this issue successfully!

This is my code for doing this functionality, i had two label for showing information(PlaceLablel and CostLablel):

private void manager_Load(object sender, EventArgs e)
{
ManagerNotifyCost MNC = new ManagerNotifyCost();
MNC.Show();
}

and MangerNotiryCost class:

public partial class ManagerNotifyCost : Form
{
delegate void SetTextCallBack(string Cost, string Place);
MySqlCommand Command = new MySqlCommand();
MySqlConnection Connection=new MySqlConnection(/*my connection string*/);
MySqlDataReader Reader;
public ManagerNotifyCost()
{
InitializeComponent();
}

private void ManagerNotifyCost_Load(object sender, EventArgs e)
{
Thread ShowCost = new Thread(new ThreadStart(ShowingCost));
ShowCost.Start();
}

private void SetText(string Cost, string Place)
{
if (PlaceLabel.InvokeRequired)
{
SetTextCallBack Text = new SetTextCallBack(SetText);
this.Invoke(Text, new object[] { Cost, Place });
}
else
{
this.CostLabel.Text = "shop";
this.PlaceLabel.Text = Cost;
break;
}
}
private void ShowingCost()
{
lock (this)
{
int Cost = 0;
Command.Connection =Connection;
while (this.Visible)
{
Connection.Open();
Cost = 0;
//this is a place that information retrieve from DB
this.SetText(Cost.ToString(), "shop");
Connection.Close();
}
}
}
}