c# - Waiting for a ThreadState to be "Stopped" in a Button Click Event -
i'm having issues while waiting threadstate during click event of button control. whenever click button it'll execute code below. problem won't wait until threadstate "stopped", never enables btnimportdata or btnexportbellijst.
i've tried t.join() freezes whole form , use richtextbox logger, that'd result in logger freezes few seconds , shows lot of text @ once. reason put importdata function on thread keep form running people can see logs happening realtime.
what i'd have when click button:
- change
enabledof 1 or more buttons. - run function
importdataon thread logger can keep logging. (void importdata(){}) change
enabledof 1 or more buttons after function done doing something.private void btnimportdata_click(object sender, eventargs e) { //disable current button btnimportdata.enabled = false; imgbonne.visible = false; //random image rtconsole.visible = true; //richtextbox logger //create new thread button function var t = new thread(importdata); t.start(); //it not wait until thread stopped while (t.threadstate == threadstate.stopped) { //never gets executed btnimportdata.enabled = true; btnexportbellijst.enabled = true; } }
extra info: screenshot before pressing "import data": http://puu.sh/88od6.png screenshot after application done importing data: http://puu.sh/88ont.png
(edit)target framework: .net framework 4
i using code below, instantly enables buttons after pressing "import data".
private void btnimportdata_click(object sender, eventargs e) { imgbonne.visible = false; //random image rtconsole.visible = true; //richtextbox logger var t = new thread(importdata); t.start(); while (t.threadstate == threadstate.running) { btnimportdata.enabled = false; } btnimportdata.enabled = true; btnexportbellijst.enabled = true; } edit: i'm sorry if in wrong category, wanted put in c#.
using task parallel library can make whole lot easier:
private void btnimportdata_click(object sender, eventargs e) { imgbonne.visible = false; //random image rtconsole.visible = true; //richtextbox logger btnimportdata.enabled = false; task.run(importdata).continuewith((task task) => { btnimportdata.enabled = true; btnexportbellijst.enabled = true; }, taskscheduler.fromcurrentsynchronizationcontext()); }
Comments
Post a Comment