|
-
Jun 5th, 2006, 06:06 AM
#1
Thread Starter
Hyperactive Member
[1.0/1.1] Accessing a form within a thread.
Hi. I'm handling messages coming into a client application. When message a1 comes in, I want to get some information and display it onto a form MainForm. To handle the incoming messages from the server, I need a thread to do so. So here's what I did:
MainForm mf = new MainForm();
public void ServerThreadRunning()
{
// Get message from server.
// Proces message and get information
mf .txtMessage.Text = infoa
mf ........
// I dislpay the information onto the MainForm.
}
However I had to change this up since after displaying the information, the MainForm was sticking and then crashed. So what I did was to create another thread with the Server thread as:
MainForm mf = new MainForm();
public void ServerThreadRunning()
{
// Get message from server.
// Proces message and get information
// Instantiate new thread, subthread.
}
public void subthreadmethod()
{
mf .txtMessage.Text = infoa
mf ........
// I dislpay the information onto the MainForm.
}
Now this works fine. The MainForm is not sticking after displaying the message. however, it seems that when subthread ends, it disposes MainForm object so if I try to use mf in another part of the program, I'm getting error: object is not instantiated. So when this inner thread ends, it also terminates my object.
How could I solve this problem? How could I access and display my information from within the thread without that thread disposing my object?
Jennifer
-
Jun 5th, 2006, 12:50 PM
#2
Re: [1.0/1.1] Accessing a form within a thread.
1- There is no such error as "object is not instantiated".
2- There is no such thing as "sub thread"
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
-
Jun 7th, 2006, 09:20 AM
#3
Thread Starter
Hyperactive Member
Re: [1.0/1.1] Accessing a form within a thread.
Ok. The term subthread is my own terminlogy. Let me explain:
Code:
public Form(MainForm f){
MainForm mf = f; // Here is where I instantiate the reference from MainForm.
// Start thread calling method ServerThreadRunning.
}
public void ServerThreadRunning()
{
// Needs to continually loop to receive message from server.
// Get message from server.
// Proces message and get information
// Instantiate a new thread calling subthreadmethod().
}
public void subthreadmethod()
{
// Inside this method I'm using the mf reference object to access component
// of the MainForm.
mf .txtMessage.Text = infoa
mf ........
// I dislpay the information onto the MainForm.
}
Its just that, whenever I use the object Mf or f or any other object associated with that memory pointer, I'm getting the following error:
An unhandled exception of type 'System.NullReferenceException' occurred in system.windows.forms.dll
Additional information: Object reference not set to an instance of an object.
So apparently what is taking place is that when the subthread (just a name) ends / aborts, it is also disposing the mf object. It looks like that object is binding together with the subthread (excuse my terminlogy).
Any Ideas how to access mf inside of the subthread so that wheen the subthread ends, the object mf will still be alive?
N.B. Mainform object mf and f are both instantiated before the subthread starts. However, after they both does not exist.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|