threading and delegates


R

RobcPettit

Hi, this is a hard question to ask without posting some code, so sorry
if its to much. Im using a control which Ive added labels. Basically I
want update the data every second. Ive rad up on threadind and now
know that I need to use a delegate with my control because it uses a
sepperate thread. I cant work out how to do it though. Heres the code
for my control.
using System;
using System.Windows.Forms;
namespace exceltest
{
/// <summary>
/// Summary description for MarketViewRow.
/// </summary>
public class MarketViewRow : System.Windows.Forms.UserControl
{


private System.Windows.Forms.Button btnLay3;
private System.Windows.Forms.Button btnLay2;
private System.Windows.Forms.Button btnLay;
private System.Windows.Forms.Button btnBack;
private System.Windows.Forms.Button btnBack2;
private System.Windows.Forms.Button btnBack3;
private System.Windows.Forms.Label lblRunner;
private System.Windows.Forms.Label label1;
private Label label2;
private Label label3;
private Label label4;
private Label label5;
private Label label6;
private Label label7;

/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;


public MarketViewRow()
{
// This call is required by the Windows.Forms Form
Designer.
InitializeComponent();


}
#region DisplayRunners
public void DisplayRunners(string runner)
{
lblRunner.Text = (runner != "")
? runner
: string.Empty;
}
#endregion


#region DisplayPrices
public void DisplayPrices(string currency, double backPrice1,
double backAmountAvailable1, double backPrice2, double
backAmountAvailable2, double backPrice3, double backAmountAvailable3,
double layPrice1, double layAmountAvailable1, double layPrice2, double
layAmountAvailable2, double layPrice3, double layAmountAvailable3)
{
btnBack.Text = (backPrice1 != 0.0)
? backPrice1.ToString() + "\n" +
backAmountAvailable1.ToString()
: string.Empty;
btnBack2.Text = (backPrice2 != 0.0)
? backPrice2.ToString() + "\n" +
backAmountAvailable2.ToString()
: string.Empty;
btnBack3.Text = (backPrice3 != 0.0)
? backPrice3.ToString() + "\n" +
backAmountAvailable3.ToString()
: string.Empty;

btnLay.Text = (layPrice1 != 0.0)
? layPrice1.ToString() + "\n" +
layAmountAvailable1.ToString()
: string.Empty;
btnLay2.Text = (layPrice2 != 0.0)
? layPrice2.ToString() + "\n" +
layAmountAvailable2.ToString()
: string.Empty;
btnLay3.Text = (layPrice3 != 0.0)
? layPrice3.ToString() + "\n" +
layAmountAvailable3.ToString()
: string.Empty;
}
#endregion

protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Component 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.btnLay3 = new System.Windows.Forms.Button();
this.btnLay2 = new System.Windows.Forms.Button();
this.btnLay = new System.Windows.Forms.Button();
this.btnBack = new System.Windows.Forms.Button();
this.btnBack2 = new System.Windows.Forms.Button();
this.btnBack3 = new System.Windows.Forms.Button();
this.lblRunner = new System.Windows.Forms.Label();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
this.label5 = new System.Windows.Forms.Label();
this.label6 = new System.Windows.Forms.Label();
this.label7 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// btnLay3
//
this.btnLay3.BackColor = System.Drawing.Color.White;
this.btnLay3.Dock = System.Windows.Forms.DockStyle.Right;
this.btnLay3.FlatStyle =
System.Windows.Forms.FlatStyle.Flat;
this.btnLay3.Font = new System.Drawing.Font("Microsoft
Sans Serif", 8F, System.Drawing.FontStyle.Regular,
System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.btnLay3.Location = new System.Drawing.Point(827, 0);
this.btnLay3.Name = "btnLay3";
this.btnLay3.Size = new System.Drawing.Size(55, 39);
this.btnLay3.TabIndex = 29;
this.btnLay3.UseVisualStyleBackColor = false;
//
// btnLay2
//
this.btnLay2.BackColor = System.Drawing.Color.White;
this.btnLay2.Dock = System.Windows.Forms.DockStyle.Right;
this.btnLay2.FlatStyle =
System.Windows.Forms.FlatStyle.Flat;
this.btnLay2.Font = new System.Drawing.Font("Microsoft
Sans Serif", 8F, System.Drawing.FontStyle.Regular,
System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.btnLay2.Location = new System.Drawing.Point(772, 0);
this.btnLay2.Name = "btnLay2";
this.btnLay2.Size = new System.Drawing.Size(55, 39);
this.btnLay2.TabIndex = 30;
this.btnLay2.UseVisualStyleBackColor = false;
//
// btnLay
//
this.btnLay.BackColor = System.Drawing.Color.Orchid;
this.btnLay.Dock = System.Windows.Forms.DockStyle.Right;
this.btnLay.FlatStyle =
System.Windows.Forms.FlatStyle.Flat;
this.btnLay.Font = new System.Drawing.Font("Microsoft Sans
Serif", 8F, System.Drawing.FontStyle.Regular,
System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.btnLay.Location = new System.Drawing.Point(717, 0);
this.btnLay.Name = "btnLay";
this.btnLay.Size = new System.Drawing.Size(55, 39);
this.btnLay.TabIndex = 31;
this.btnLay.UseVisualStyleBackColor = false;
//
// btnBack
//
this.btnBack.BackColor = System.Drawing.Color.LightBlue;
this.btnBack.Dock = System.Windows.Forms.DockStyle.Right;
this.btnBack.FlatStyle =
System.Windows.Forms.FlatStyle.Flat;
this.btnBack.Font = new System.Drawing.Font("Microsoft
Sans Serif", 8F, System.Drawing.FontStyle.Regular,
System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.btnBack.Location = new System.Drawing.Point(662, 0);
this.btnBack.Name = "btnBack";
this.btnBack.Size = new System.Drawing.Size(55, 39);
this.btnBack.TabIndex = 32;
this.btnBack.UseVisualStyleBackColor = false;
//
// btnBack2
//
this.btnBack2.BackColor = System.Drawing.Color.White;
this.btnBack2.Dock = System.Windows.Forms.DockStyle.Right;
this.btnBack2.FlatStyle =
System.Windows.Forms.FlatStyle.Flat;
this.btnBack2.Font = new System.Drawing.Font("Microsoft
Sans Serif", 8F, System.Drawing.FontStyle.Regular,
System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.btnBack2.Location = new System.Drawing.Point(607, 0);
this.btnBack2.Name = "btnBack2";
this.btnBack2.Size = new System.Drawing.Size(55, 39);
this.btnBack2.TabIndex = 33;
this.btnBack2.UseVisualStyleBackColor = false;
//
// btnBack3
//
this.btnBack3.BackColor = System.Drawing.Color.White;
this.btnBack3.Dock = System.Windows.Forms.DockStyle.Right;
this.btnBack3.FlatStyle =
System.Windows.Forms.FlatStyle.Flat;
this.btnBack3.Font = new System.Drawing.Font("Microsoft
Sans Serif", 8F, System.Drawing.FontStyle.Regular,
System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.btnBack3.Location = new System.Drawing.Point(552, 0);
this.btnBack3.Name = "btnBack3";
this.btnBack3.Size = new System.Drawing.Size(55, 39);
this.btnBack3.TabIndex = 34;
this.btnBack3.UseVisualStyleBackColor = false;
//
// lblRunner
//
this.lblRunner.BorderStyle =
System.Windows.Forms.BorderStyle.FixedSingle;
this.lblRunner.Dock = System.Windows.Forms.DockStyle.Fill;
this.lblRunner.Font = new System.Drawing.Font("Microsoft
Sans Serif", 9F, System.Drawing.FontStyle.Bold,
System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.lblRunner.Location = new System.Drawing.Point(2, 0);
this.lblRunner.Name = "lblRunner";
this.lblRunner.Size = new System.Drawing.Size(550, 39);
this.lblRunner.TabIndex = 35;
this.lblRunner.TextAlign =
System.Drawing.ContentAlignment.MiddleLeft;
//
// label1
//
this.label1.BorderStyle =
System.Windows.Forms.BorderStyle.FixedSingle;
this.label1.Dock = System.Windows.Forms.DockStyle.Right;
this.label1.Font = new System.Drawing.Font("Microsoft Sans
Serif", 8F, System.Drawing.FontStyle.Bold,
System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.label1.Location = new System.Drawing.Point(497, 0);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(55, 39);
this.label1.TabIndex = 36;
this.label1.TextAlign =
System.Drawing.ContentAlignment.MiddleLeft;
//
// label2
//
this.label2.BorderStyle =
System.Windows.Forms.BorderStyle.FixedSingle;
this.label2.Dock = System.Windows.Forms.DockStyle.Right;
this.label2.Font = new System.Drawing.Font("Microsoft Sans
Serif", 9F, System.Drawing.FontStyle.Bold,
System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.label2.Location = new System.Drawing.Point(442, 0);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(55, 39);
this.label2.TabIndex = 37;
this.label2.TextAlign =
System.Drawing.ContentAlignment.MiddleLeft;
//
// label3
//
this.label3.BorderStyle =
System.Windows.Forms.BorderStyle.FixedSingle;
this.label3.Dock = System.Windows.Forms.DockStyle.Right;
this.label3.Font = new System.Drawing.Font("Microsoft Sans
Serif", 9F, System.Drawing.FontStyle.Bold,
System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.label3.Location = new System.Drawing.Point(387, 0);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(55, 39);
this.label3.TabIndex = 38;
this.label3.TextAlign =
System.Drawing.ContentAlignment.MiddleLeft;
this.label3.Click += new
System.EventHandler(this.label3_Click);
//
// label4
//
this.label4.BorderStyle =
System.Windows.Forms.BorderStyle.FixedSingle;
this.label4.Dock = System.Windows.Forms.DockStyle.Right;
this.label4.Font = new System.Drawing.Font("Microsoft Sans
Serif", 9F, System.Drawing.FontStyle.Bold,
System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.label4.Location = new System.Drawing.Point(332, 0);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(55, 39);
this.label4.TabIndex = 39;
this.label4.TextAlign =
System.Drawing.ContentAlignment.MiddleLeft;
//
// label5
//
this.label5.BorderStyle =
System.Windows.Forms.BorderStyle.FixedSingle;
this.label5.Dock = System.Windows.Forms.DockStyle.Right;
this.label5.Font = new System.Drawing.Font("Microsoft Sans
Serif", 9F, System.Drawing.FontStyle.Bold,
System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.label5.Location = new System.Drawing.Point(277, 0);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(55, 39);
this.label5.TabIndex = 40;
this.label5.TextAlign =
System.Drawing.ContentAlignment.MiddleLeft;
//
// label6
//
this.label6.BorderStyle =
System.Windows.Forms.BorderStyle.FixedSingle;
this.label6.Dock = System.Windows.Forms.DockStyle.Right;
this.label6.Font = new System.Drawing.Font("Microsoft Sans
Serif", 9F, System.Drawing.FontStyle.Bold,
System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.label6.Location = new System.Drawing.Point(222, 0);
this.label6.Name = "label6";
this.label6.Size = new System.Drawing.Size(55, 39);
this.label6.TabIndex = 41;
this.label6.TextAlign =
System.Drawing.ContentAlignment.MiddleLeft;
//
// label7
//
this.label7.BorderStyle =
System.Windows.Forms.BorderStyle.FixedSingle;
this.label7.Dock = System.Windows.Forms.DockStyle.Right;
this.label7.Font = new System.Drawing.Font("Microsoft Sans
Serif", 9F, System.Drawing.FontStyle.Bold,
System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.label7.Location = new System.Drawing.Point(167, 0);
this.label7.Name = "label7";
this.label7.Size = new System.Drawing.Size(55, 39);
this.label7.TabIndex = 42;
this.label7.TextAlign =
System.Drawing.ContentAlignment.MiddleLeft;
//
// MarketViewRow
//
this.AutoValidate =
System.Windows.Forms.AutoValidate.Disable;
this.BackColor = System.Drawing.Color.White;
this.Controls.Add(this.label7);
this.Controls.Add(this.label6);
this.Controls.Add(this.label5);
this.Controls.Add(this.label4);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.lblRunner);
this.Controls.Add(this.btnBack3);
this.Controls.Add(this.btnBack2);
this.Controls.Add(this.btnBack);
this.Controls.Add(this.btnLay);
this.Controls.Add(this.btnLay2);
this.Controls.Add(this.btnLay3);
this.Font = new System.Drawing.Font("Microsoft Sans
Serif", 6F, System.Drawing.FontStyle.Regular,
System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.Name = "MarketViewRow";
this.Padding = new System.Windows.Forms.Padding(2, 0, 2,
2);
this.Size = new System.Drawing.Size(884, 41);
this.Load += new
System.EventHandler(this.MarketViewRow_Load);
this.ResumeLayout(false);

}
#endregion

private void MarketViewRow_Load(object sender, EventArgs e)
{

}

private void label3_Click(object sender, EventArgs e)
{

}




}
}


From my main form The code basically works out how many rows I need
added the this code adds the data;
public void addprices()
{


datafile dta = new datafile();

string fileName = labelRunnerInfo.Text;
int number = m_marketViewRows.Count;
for (int i = 0; i < 1; i++)
{

for (int z = 0; z < number; z++)
{
string rowName = z.ToString();
MarketViewRow row =
(MarketViewRow)m_marketViewRows[rowName];
dta.ReadFromFile(fileName);
string list = dta.sLine;
string[] astring = list.Split(',');
dta.numberofscippedLines++;


string currency = "uk";
double backPrice1 = double.Parse(astring[3]);
double backAmountAvailable1 =
double.Parse(astring[4]);
double backPrice2 = double.Parse(astring[5]);
double backAmountAvailable2 =
double.Parse(astring[6]);
double backPrice3 = double.Parse(astring[7]);
double backAmountAvailable3 =
double.Parse(astring[8]);
double layPrice1 = double.Parse(astring[9]);
double layAmountAvailable1 =
double.Parse(astring[10]);
double layPrice2 = double.Parse(astring[11]);
double layAmountAvailable2 =
double.Parse(astring[12]);
double layPrice3 = double.Parse(astring[13]);
double layAmountAvailable3 =
double.Parse(astring[14]);

row.DisplayPrices(currency, backPrice1,
backAmountAvailable1, backPrice2, backAmountAvailable2, backPrice3,
backAmountAvailable3, layPrice1, layAmountAvailable1, layPrice2,
layAmountAvailable2, layPrice3, layAmountAvailable3);

}


}
}


the ... row.DisplayPrices(currency,
backPrice1, backAmountAvailable1, backPrice2, backAmountAvailable2,
backPrice3, backAmountAvailable3, layPrice1, layAmountAvailable1,
layPrice2, layAmountAvailable2, layPrice3, layAmountAvailable3);
is the information thats Iwant to add each second to the control. I
cant work out how to set the delagate up. So far Im thinking in my
control I need to add something like ' public delegate my delegate
void onDisplayPrices(....same signature as diplay prices).
Id really like some help on this. Sorry for so much code.
Regards Robert
 
Ad

Advertisements

J

Jon Skeet [C# MVP]

Hi, this is a hard question to ask without posting some code, so sorry
if its to much. Im using a control which Ive added labels. Basically I
want update the data every second. Ive rad up on threadind and now
know that I need to use a delegate with my control because it uses a
sepperate thread. I cant work out how to do it though. Heres the code
for my control.

the ... row.DisplayPrices(currency,
backPrice1, backAmountAvailable1, backPrice2, backAmountAvailable2,
backPrice3, backAmountAvailable3, layPrice1, layAmountAvailable1,
layPrice2, layAmountAvailable2, layPrice3, layAmountAvailable3);
is the information thats Iwant to add each second to the control. I
cant work out how to set the delagate up. So far Im thinking in my
control I need to add something like ' public delegate my delegate
void onDisplayPrices(....same signature as diplay prices).
Id really like some help on this. Sorry for so much code.

As you say, you've provided an awful lot of code. Any chance you could
try to cut it down to a much smaller sample so we can see the wood for
the trees?

See http://pobox.com/~skeet/csharp/threads/winforms.shtml for some
sample code.
 
R

RobcPettit

Thankyou for your reply and link. Im working through that now. When I
run my code with a thread as it is, I get an error saying cant use
same thread as control created on. So Im trying to send the results
with the ...
row.DisplayPrices(currency,
backPrice1, backAmountAvailable1, backPrice2, backAmountAvailable2,
backPrice3, backAmountAvailable3, layPrice1, layAmountAvailable1,
layPrice2, layAmountAvailable2, layPrice3, layAmountAvailable3);
to my control through;
#region DisplayPrices
public void DisplayPrices(string currency, double backPrice1,
double backAmountAvailable1, double backPrice2, double
backAmountAvailable2, double backPrice3, double backAmountAvailable3,
double layPrice1, double layAmountAvailable1, double layPrice2,
double
layAmountAvailable2, double layPrice3, double layAmountAvailable3)
{
btnBack.Text = (backPrice1 != 0.0)
? backPrice1.ToString() + "\n" +
backAmountAvailable1.ToString()
: string.Empty;
btnBack2.Text = (backPrice2 != 0.0)
? backPrice2.ToString() + "\n" +
backAmountAvailable2.ToString()
: string.Empty;
btnBack3.Text = (backPrice3 != 0.0)
? backPrice3.ToString() + "\n" +
backAmountAvailable3.ToString()
: string.Empty;


btnLay.Text = (layPrice1 != 0.0)
? layPrice1.ToString() + "\n" +
layAmountAvailable1.ToString()
: string.Empty;
btnLay2.Text = (layPrice2 != 0.0)
? layPrice2.ToString() + "\n" +
layAmountAvailable2.ToString()
: string.Empty;
btnLay3.Text = (layPrice3 != 0.0)
? layPrice3.ToString() + "\n" +
layAmountAvailable3.ToString()
: string.Empty;
}
#endregion
Its this part that updates the display, so Im thinking that Ive got to
wrap this in a delegate to r interupt the control thread.
Regards Robert
 
J

Jon Skeet [C# MVP]

Thankyou for your reply and link. Im working through that now. When I
run my code with a thread as it is, I get an error saying cant use
same thread as control created on. So Im trying to send the results
with the ...

Its this part that updates the display, so Im thinking that Ive got to
wrap this in a delegate to r interupt the control thread.

Yes, exactly. See the examples of Control.Invoke/BeginInvoke in the
page that I linked to before.
 
Ad

Advertisements

R

RobcPettit

Hi, I know its a while but I thought I d just let you know Ive cracked
it. Thanks for the link, a great help.
Regards Robert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top