getwayworker timer,BackgroundWorker的线程和定时器逻辑

I've been trying to get the logic right for my timer and backgroundworker thread. Granted I don't fully understand the whole system despite all my reading. the following are excerpts of code concerned:

My polling button :

private void pollStart_Click(object sender, EventArgs e)

{

tst_bgw = new BackgroundWorker();

//mandatory. Otherwise will throw an exception when calling ReportProgress method

tst_bgw.WorkerReportsProgress = true;

//mandatory. Otherwise we would get an InvalidOperationException when trying to cancel the operation

tst_bgw.WorkerSupportsCancellation = true;

tst_bgw.DoWork += tst_bgw_DoWork;

tst_bgw.ProgressChanged += tst_bgw_ProgressChanged;

tst_bgw.RunWorkerCompleted += tst_bgw_RunWorkerCompleted;

tst_bgw.RunWorkerAsync();

}

which I think is right so far

my Background worker thread:

private void tst_bgw_DoWork(object source, DoWorkEventArgs e)

{

m_timer = new System.Timers.Timer();

m_timer.Interval = 1000;

m_timer.Enabled = true;

m_timer.Elapsed += new ElapsedEventHandler(OnTimedEvent);

if (tst_bgw.CancellationPending)

{

e.Cancel = true;

return;

}

}

and the elapsed tier event code:

private void OnTimedEvent(object source, ElapsedEventArgs e)

{

if (powerVal > 3250)

{

m_timer.Stop();

tst_bgw.CancelAsync();

}

else

{

string pow;

int progressVal = 100 - ((3250 - powerVal) / timerVal);

uiDelegateTest tstDel = new uiDelegateTest(recvMessage);// the recvMessage function takes a textbox as an argument and directs output from socket to it.

pow = construct_command("power", powerVal);

sData = Encoding.ASCII.GetBytes(pow);

if (active_connection)

try

{

m_sock.Send(sData);

Array.Clear(sData, 0, sData.Length);

tstDel(ref unit_Output);// Read somewhere that you can only modify UI elements in this method via delegate so I think this is OK.

m_sock.Send(time_out_command);

tstDel(ref unit_Output);

tst_bgw.ReportProgress(progressVal);

}

catch (SocketException se)

{

MessageBox.Show(se.Message);

}

tst_bgw.ReportProgress(powerVal, progressVal);

powerVal = powerVal + pwrIncVal;

}

I'd just like to know a few other things; am I using the right timer (not that I think it should matter greatly but it was suggested that this might be the best timer for what I want to do) and canI really modify UI elements in the DoWork method only through delegates and if yes are there sepcial considerations to doing so.

Sorry about the long posting and thank you for your time.

解决方案

There is lots wrong with this code.

1) You aren't disposing of your background worker. BackgroundWorkers must be disposed of after use. They are designed to be used as winforms components and would normally be added to a window via the designer. This will ensure it is created with the form and disposed of when the form is.

2) All you are doing in your dowork method is creating a new timer and running it. There is no point of doing this in a background worker because it will happen so quickly anyway.

3) You will recreate the timer every time you run the background worker again. But you aren't ever stopping or disposing of the old timer, you are just overwriting the member.

I recommend you get rid of the BackgroundWorker completely and just use a timer. Create the timer in the forms constructor and make sure you dispose of it in the forms dispose method. (Or use the designer to add it to the form). In the pollstart_click method just start the timer. (If you have a poll stop method, you can stop the timer in that)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值