关于在Winform中新建线程的问题

lsxsxs 2010-04-01 04:46:55
这里人气比较高,来这里问下。我用的是VS2008,建的程序是个Winform,但是用的是VC++语言。想点击一个按钮之后把数据添加到Access数据库里去,但是由于数据量大,程序就直接死了。想在按钮事件中新建一个线程来添加。但是始终有问题,就在ThreadStart的时候。
ThreadStart^ threadDelegate = gcnew ThreadStart(Add());
Thread^ thread = gcnew Thread(threadDelegate);
thread->Start();

其中Add是一个函数,查找了一些C#的方法,把Add后面的括号去了也不行。谁能帮忙解决一下啊。谢谢
...全文
84 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
lsxsxs 2010-04-03
  • 打赏
  • 举报
回复
原来Winform里有个backgroundWork控件,用这个可以进行后台操作。公司太畸形了,要用这个四不像的东西开发。
wenbin 2010-04-01
  • 打赏
  • 举报
回复

// [C++]
// Compile using /clr option.
using namespace System;
using namespace System::Threading;

// Simple threading scenario: Start a Shared method running
// on a second thread.
public ref class ThreadExample
{
public:

// The ThreadProc method is called when the thread starts.
// It loops ten times, writing to the console and yielding
// the rest of its time slice each time, and then ends.
static void ThreadProc()
{
for ( int i = 0; i < 10; i++ )
{
Console::Write( "ThreadProc: " );
Console::WriteLine( i );

// Yield the rest of the time slice.
Thread::Sleep( 0 );

}
}

};

int main()
{
Console::WriteLine( "Main thread: Start a second thread." );

// Create the thread, passing a ThreadStart delegate that
// represents the ThreadExample::ThreadProc method. For a
// delegate representing a static method, no object is
// required.
Thread^ oThread = gcnew Thread( gcnew ThreadStart( &ThreadExample::ThreadProc ) );

// Start the thread. On a uniprocessor, the thread does not get
// any processor time until the main thread yields. Uncomment
// the Thread.Sleep that follows t.Start() to see the difference.
oThread->Start();

//Thread::Sleep(0);
for ( int i = 0; i < 4; i++ )
{
Console::WriteLine( "Main thread: Do some work." );
Thread::Sleep( 0 );

}
Console::WriteLine( "Main thread: Call Join(), to wait until ThreadProc ends." );
oThread->Join();
Console::WriteLine( "Main thread: ThreadProc.Join has returned. Press Enter to end program." );
Console::ReadLine();
return 0;
}


以上是MSDN的例子
mjay0210 2010-04-01
  • 打赏
  • 举报
回复
vc.net 我真觉得是费力不讨好!
你还是用C#写这段吧。
lsxsxs 2010-04-01
  • 打赏
  • 举报
回复
C#这样是行,不过我用的是C++,不加括号的话,还会多一个错误,就是Add函数错误。这里有C++开发Winform的人吗
缭绕飘渺 2010-04-01
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 xiaoqiang321 的回复:]
Thread t = new Thread(new ThreadStart(Add));
t.Start();
[/Quote]
这样就可以的
飞天赤狐 2010-04-01
  • 打赏
  • 举报
回复
Thread t = new Thread(new ThreadStart(Add));
t.Start();

110,539

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧