Monday, 9 September 2013

Form freezes when opened for second time

Form freezes when opened for second time

I am developing a Windows Forms application that access a WCF service. I
ran into a great problem that I can't predict the reason of it. Even the
Visual Studio debugger not showing any exception in the Output view. The
scenario is like this, I have a custom user control that has a linkLabel
on it. Whenever the link label is clicked, a form is opened and a class
object is passed to it. The class definition of this object resides on WCF
service on a remote server. Now the problem is that when I click the
linkLabel, the form opens perfectly loading each of its component
according to the class object passed to it. But when I close this form and
click that linkLabel again, the form opens but immediately freezes after
loading some elements. I tried many code variations. Edited many part of
code that I think can affect. But none of them showed the difference.
Since, I don't know where actually is the code has error, I am posting the
linkLabel click code and functions that are called after it is clicked.
private void linkLabel1_LinkClicked(object sender,
LinkLabelLinkClickedEventArgs e)
{
Enabled = false;
string temp = Title.Text;
Title.Text = "Opening...";
System.Threading.Thread t = new System.Threading.Thread(new
System.Threading.ThreadStart(openTopic));
t.Start();
Title.Text = temp;
Enabled = true;
}
void createTopicWindow()
{
TopicViewer t = new TopicViewer(t);
Invoke(new Action(() => t.Show()));
}
private void openTopic()
{
Invoke(new Action(() => createTopicWindow()));
}
The above is the edited code, since I was getting Cross thread exception
before.
Following is the code of constructor of the form that is called when
clicked the linkLabel:
try
{
InitializeComponent();
this.t = topic;
if (IsHandleCreated == false)
CreateHandle();
System.Threading.Thread th = new System.Threading.Thread(new
System.Threading.ThreadStart(loadTopic));
th.Start();
Common.openedTopics.Add(this);
AddComment addComment1 = new AddComment();
addComment1.Topic = t;
addComment1.Dock = DockStyle.Fill;
panel5.Controls.Add(addComment1);
backgroundWorker1.RunWorkerAsync();
}
catch (Exception)
{ }
void loadTopic()
{
Invoke(new Action(()=>tHead = new TopicHeader()));
Global.SetControlPropertyThreadSafe(tHead,"Topic", t);
Global.SetControlPropertyThreadSafe(tHead,"Dock", DockStyle.Fill);
Invoke(new Action(()=>panel1.Controls.Add(tHead)));
Global.SetControlPropertyThreadSafe(this,"Text", t.Title + " - Topic
Viewer");
if (t.Description.Trim().Length > 0)
{
Global.SetControlPropertyThreadSafe(webBrowser1, "DocumentText",
t.Description);
}
else
{
Invoke(new Action(() => tabControl1.TabPages[0].Dispose()));
}
Global.SetControlPropertyThreadSafe(tabPage2, "Text", "Comments (" +
client.getComCount(t.TopicID) + ") ");
}
TopicHeader is another small user control. Please anyone tell me the
solution to this?

No comments:

Post a Comment