在Windows中单击button_click时出现多线程问题. [英] Multithreading probleam when button_click in windows.

查看:71
本文介绍了在Windows中单击button_click时出现多线程问题.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
我需要在button_click事件上实现多线程.我正在按击操作,我正在更新数据库值.如果我的客户端连续单击而没有中断,我需要在button_click中创建线程添加,并且我需要用于更新数据库值的调用方法.任何人都可以帮助我.
谢谢,
Naresh.G

Hi all,
I need to implement Multithreading on button_click event.i am doing on buttion click i am updateing database values.if one my client is clicking continuesly with out break i need to create thread add in button_click and i need call method for update database value.can any one help me in this.
Thanks,
Naresh.G

推荐答案

以下是使用button_click事件执行多线程的示例代码

Here is the sample code to execute multi threading using button_click events

public void ThreadRun( )
{
  // ur multi thread operation
}

Thread oThread = new Thread(new ThreadStart(ThreadRun));

private void Start_Click(object sender, EventArgs e)
{
    oThread.Start();
}

private void Cancel_Click(object sender, EventArgs e)
{
    oThread.Abort();

}


看看System.Threading.Tasks.Task类,它使处理任务更加容易并且还支持取消操作.

Uros
Take a look at the System.Threading.Tasks.Task class which makes it easier to work with tasks and also supports cancelation.

Uros


使用系统;
使用System.Collections.Generic;
使用System.ComponentModel;
使用System.Data;
使用System.Drawing;
使用System.Linq;
使用System.Text;
使用System.Windows.Forms;
使用System.Threading;

命名空间WindowsFormsApplication6
{
公共局部类Form1:Form
{
int _digitsToCalc = 20;
私有线程currentTimeThread = null;
公共Form1()
{
InitializeComponent();

currentTimeThread =新线程(新ThreadStart(CountTime));
currentTimeThread.IsBackground = true;
}


公共无效CountTime()
{
而(true)
{
调用(新的UpdateTimeDelegate(updateCurrentTime));
Thread.Sleep(1000);
}
}
私有void updateCurrentTime()
{
randomNumbers.Text = DateTime.Now.ToLongTimeString();

currentTimeThread.Abort();
}
静态列表< string> _list =新列表< string>();
私有void start_Click(对象发送者,EventArgs e)
{

currentTimeThread.Start();//错误是:线程正在运行或终止;它无法重新启动.

}
}
公共委托void UpdateTimeDelegate();
}


所有这些我的代码,我得到这样的错误:线程正在运行或终止;它无法重新启动.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace WindowsFormsApplication6
{
public partial class Form1 : Form
{
int _digitsToCalc = 20;
private Thread currentTimeThread = null;
public Form1()
{
InitializeComponent();

currentTimeThread = new Thread(new ThreadStart(CountTime));
currentTimeThread.IsBackground = true;
}


public void CountTime()
{
while (true)
{
Invoke(new UpdateTimeDelegate(updateCurrentTime));
Thread.Sleep(1000);
}
}
private void updateCurrentTime()
{
randomNumbers.Text = DateTime.Now.ToLongTimeString();

currentTimeThread.Abort();
}
static List <string> _list = new List <string>();
private void start_Click(object sender, EventArgs e)
{

currentTimeThread.Start();//error is:Thread is running or terminated; it cannot restart.

}
}
public delegate void UpdateTimeDelegate();
}


Hi all this my code and i am geting error like this:Thread is running or terminated; it cannot restart.


这篇关于在Windows中单击button_click时出现多线程问题.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆