TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Guest User
Tech Writer
3
787
C# Threading Error
Mar 15 2018 3:08 AM
Any other ways to do it in C# ? I've been working for it since yesterday but i cant solve this problem. . I want this code to be executed in the background using another thread so that my main thread or UI thread will be responsive, I am creating a dynamic label using loop. . Thanks.
private
void
Contact_Load(
object
sender, EventArgs e)
{
Thread loadContact =
new
Thread(
new
ThreadStart(loadContacts));
loadContact.Start();
}
private
void
loadContacts()
{
Label l =
null
; ContactPanel.Controls.Clear();
dt.Clear();
dataConnection.Open();
using
(var messages =
new
SqlCommand(
"SELECT * FROM BasicInfo "
, dataConnection))
{
da.SelectCommand = messages; da.Fill(dt);
SqlDataReader rd = messages.ExecuteReader();
string
Fname, Lname,Fullname,Id;
if
(rd.HasRows)
{
for
(
int
i = 0; i < dt.Rows.Count; i++)
{
Point location = Point.Empty;
DataRow dr = dt.Rows[i];
Id = dr[
"_Id"
].ToString();
Fname = dr[
"_Fname"
].ToString();
Lname = dr[
"_Lname"
].ToString();
Fullname = Fname + Lname;
l = addLabel(i);
ContactPanel.Controls.Add(l);
l.Text = Fname +
" "
+ Lname; l.Name = Lname + i;
l.Click +=
new
EventHandler(
this
.labelClick);
}
}
else
{ }
Thread.Sleep(5000);
}
dataConnection.Close();
}
public
void
labelClick(
object
sender, EventArgs e)
{
Label currentLabel = (Label)sender;
currentLabel.BackColor = Color.Gray;
MessageBox.Show(currentLabel.Text);
MainForm MainForm =
new
MainForm();
MainForm.Show();
this
.Close();
MainForm.TxtReciever.Text = currentLabel.Text;
}
public
Label addLabel(
int
i)
{
Label l =
new
Label();
l.Name =
"Date"
+ i.ToString();
l.BackColor = Color.SteelBlue;
l.ForeColor = Color.White;
//l.BackColor = Color.White;
l.Font =
new
Font(
"Segoe UI"
, 12, FontStyle.Regular);
l.TextAlign = ContentAlignment.MiddleCenter;
l.Margin =
new
Padding(5,3,3,3);
l.Width = 255;
l.Height = 53;
return
l;
}
Reply
Answers (
2
)
Use Of PIVOT in SQL Server without aggregate function
How To Call On Click Event In MVC 5