Multithreading in Form

G

Guest

Hi All,

I am new to C# thread programming. I got one issue. I appreciate if
anybody can point what i am doing wrong here.

I have created one C#.NET application (VS.NET 2003 and 1.1 Framework).

It contains two forms. (Form1 and Form2)
Form1 contains only one button.
On click of this button two threads will be created.
This thread will just display the Form2.

Form2 contains only one ActiveX control(Created by me, Works fine in single
thread app for last 3-4 years).

When i excute this application, if i click on the Button on Form1, Threads
are created and it shows the Form2 and ActiveX control. So far its fine.

When i close the Forms created by the thread it crashes.
When i use the Debuger it says "User Break Point Called" and Call Stack
shows following :

NTDLL! 7c901230()
_CrtDbgReport(int 2, const char * 0x5f4d0cf0 THIS_FILE, int 374, const char
* 0x00000000, const char * 0x00000000) line 353
AfxAssertFailedLine(const char * 0x5f4d0cf0 THIS_FILE, int 374) line 39 + 20
bytes
AfxWndProc(HWND__ * 0x0141056a, unsigned int 8, unsigned int 16057944, long
0) line 374 + 21 bytes
AfxWndProcDllStatic(HWND__ * 0x0141056a, unsigned int 8, unsigned int
16057944, long 0) line 57 + 21 bytes
USER32! 77d48734()
USER32! 77d48816()
USER32! 77d4c63f()
USER32! 77d4c665()
SYSTEM.WINDOWS.FORMS! 7b823b1e()
SYSTEM.WINDOWS.FORMS! 7b823a2b()
SYSTEM.WINDOWS.FORMS! 7b896ca4()
SYSTEM.WINDOWS.FORMS! 7b82291b()
0096453a()
USER32! 77d48816()
USER32! 77d4b4c0()
USER32! 77d4b50c()
NTDLL! 7c90eae3()
USER32! 77d48734()
USER32! 77d5418d()
USER32! 77d53fd9()
USER32! 77d5b82a()
USER32! 77d54e28()
USER32! 77d561b3()
USER32! 77d6a92e()
USER32! 77d6a294()
USER32! 77d95fbb()
USER32! 77d96060()
USER32! 77d80577()
USER32! 77d8052f()
__crtMessageBoxA(const char * 0x032fb9c8, const char * 0x102579a0 `string',
unsigned int 73746) line 65
CrtMessageWindow(int 2, const char * 0x5f4d0cf0 THIS_FILE, const char *
0x032fcafc, const char * 0x00000000, const char * 0x032feb20) line 520 + 22
bytes
_CrtDbgReport(int 2, const char * 0x5f4d0cf0 THIS_FILE, int 969, const char
* 0x00000000, const char * 0x00000000) line 419 + 76 bytes
AfxAssertFailedLine(const char * 0x5f4d0cf0 THIS_FILE, int 969) line 39 + 20
bytes
CWnd::DestroyWindow() line 969 + 21 bytes
COleControl::OnFinalRelease() line 358
CCmdTarget::InternalRelease() line 223
CCmdTarget::ExternalRelease() line 234
COleControl::XQuickActivate::Release(COleControl::XQuickActivate * const
0x03356630) line 69
OLE32! 7750de71()
OLE32! 7750dbd2()
OLE32! 77554a4f()
OLE32! 77554a30()
OLE32! 77564daf()
OLE32! 7750221e()
OLE32! 77502171()
OLE32! 774ff221()
OLE32! 774fee88()
OLE32! 775131f0()
OLE32! 774fd141()
OLE32! 774fd0e9()
NTDLL! 7c9011a7()
NTDLL! 7c919213()
KERNEL32! 7c80cce7()
KERNEL32! 7c80b511


My C#.Net app source code looks like this:

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}


void MyThreadFunction()
{
Form frm = new Form2();
//Show the Form which contains the SymFacX. Here "using" is the keyword
which will release the frm object(Form2) once its job is done.
//If we didnt release this frm object. ActiveX will throw an excetion. As
SymFacX will try to delete itself but still frm object contains the reference.
using(frm)
{
frm.ShowDialog();
}

//frm.Dispose();
//GC.Collect();
}


private void button1_Click(object sender, System.EventArgs e)
{

//Create Thread
newThread1 = new Thread(new ThreadStart(MyThreadFunction));

//Start the Thread
newThread1.Start();

//Create Thread
newThread2 = new Thread(new ThreadStart(MyThreadFunction));

//Start the Thread
newThread2.Start();

}

private void Form1_Closing(object sender,
System.ComponentModel.CancelEventArgs e)
{
newThread1.Join(0);
newThread2.Join(0);
Environment.Exit(0);
}
 
D

Dmytro Lapshyn [MVP]

Hi,

There's an excellent resource on multithreading in Windows Forms
applications created by fellow MVPs:

http://dotnet.mvps.org/dotnet/faqs/?id=multithreading&lang=en

Make sure to check this out first.

Also, as a general rule, do not create forms on worker threads, especially
if those forms contain ActiveX controls. ActiveX controls and components are
REALLY picky about threading, and running them in a wrong threading model
can cause all sorts of crashes.

Sudha said:
Hi All,

I am new to C# thread programming. I got one issue. I appreciate if
anybody can point what i am doing wrong here.

I have created one C#.NET application (VS.NET 2003 and 1.1 Framework).

It contains two forms. (Form1 and Form2)
Form1 contains only one button.
On click of this button two threads will be created.
This thread will just display the Form2.

Form2 contains only one ActiveX control(Created by me, Works fine in
single
thread app for last 3-4 years).

When i excute this application, if i click on the Button on Form1, Threads
are created and it shows the Form2 and ActiveX control. So far its fine.

When i close the Forms created by the thread it crashes.
When i use the Debuger it says "User Break Point Called" and Call Stack
shows following :

NTDLL! 7c901230()
_CrtDbgReport(int 2, const char * 0x5f4d0cf0 THIS_FILE, int 374, const
char
* 0x00000000, const char * 0x00000000) line 353
AfxAssertFailedLine(const char * 0x5f4d0cf0 THIS_FILE, int 374) line 39 +
20
bytes
AfxWndProc(HWND__ * 0x0141056a, unsigned int 8, unsigned int 16057944,
long
0) line 374 + 21 bytes
AfxWndProcDllStatic(HWND__ * 0x0141056a, unsigned int 8, unsigned int
16057944, long 0) line 57 + 21 bytes
USER32! 77d48734()
USER32! 77d48816()
USER32! 77d4c63f()
USER32! 77d4c665()
SYSTEM.WINDOWS.FORMS! 7b823b1e()
SYSTEM.WINDOWS.FORMS! 7b823a2b()
SYSTEM.WINDOWS.FORMS! 7b896ca4()
SYSTEM.WINDOWS.FORMS! 7b82291b()
0096453a()
USER32! 77d48816()
USER32! 77d4b4c0()
USER32! 77d4b50c()
NTDLL! 7c90eae3()
USER32! 77d48734()
USER32! 77d5418d()
USER32! 77d53fd9()
USER32! 77d5b82a()
USER32! 77d54e28()
USER32! 77d561b3()
USER32! 77d6a92e()
USER32! 77d6a294()
USER32! 77d95fbb()
USER32! 77d96060()
USER32! 77d80577()
USER32! 77d8052f()
__crtMessageBoxA(const char * 0x032fb9c8, const char * 0x102579a0
`string',
unsigned int 73746) line 65
CrtMessageWindow(int 2, const char * 0x5f4d0cf0 THIS_FILE, const char *
0x032fcafc, const char * 0x00000000, const char * 0x032feb20) line 520 +
22
bytes
_CrtDbgReport(int 2, const char * 0x5f4d0cf0 THIS_FILE, int 969, const
char
* 0x00000000, const char * 0x00000000) line 419 + 76 bytes
AfxAssertFailedLine(const char * 0x5f4d0cf0 THIS_FILE, int 969) line 39 +
20
bytes
CWnd::DestroyWindow() line 969 + 21 bytes
COleControl::OnFinalRelease() line 358
CCmdTarget::InternalRelease() line 223
CCmdTarget::ExternalRelease() line 234
COleControl::XQuickActivate::Release(COleControl::XQuickActivate * const
0x03356630) line 69
OLE32! 7750de71()
OLE32! 7750dbd2()
OLE32! 77554a4f()
OLE32! 77554a30()
OLE32! 77564daf()
OLE32! 7750221e()
OLE32! 77502171()
OLE32! 774ff221()
OLE32! 774fee88()
OLE32! 775131f0()
OLE32! 774fd141()
OLE32! 774fd0e9()
NTDLL! 7c9011a7()
NTDLL! 7c919213()
KERNEL32! 7c80cce7()
KERNEL32! 7c80b511


My C#.Net app source code looks like this:

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}


void MyThreadFunction()
{
Form frm = new Form2();
//Show the Form which contains the SymFacX. Here "using" is the keyword
which will release the frm object(Form2) once its job is done.
//If we didnt release this frm object. ActiveX will throw an excetion. As
SymFacX will try to delete itself but still frm object contains the
reference.
using(frm)
{
frm.ShowDialog();
}

//frm.Dispose();
//GC.Collect();
}


private void button1_Click(object sender, System.EventArgs e)
{

//Create Thread
newThread1 = new Thread(new ThreadStart(MyThreadFunction));

//Start the Thread
newThread1.Start();

//Create Thread
newThread2 = new Thread(new ThreadStart(MyThreadFunction));

//Start the Thread
newThread2.Start();

}

private void Form1_Closing(object sender,
System.ComponentModel.CancelEventArgs e)
{
newThread1.Join(0);
newThread2.Join(0);
Environment.Exit(0);
}
 

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