BackgroundWorker 后台进程控制窗体label、richtextbook内容刷新

原文链接:http://www.cnblogs.com/WMTcore/p/3841055.html

    昨天写了一个从文章中提取关键词的程序,写完处理的逻辑后又花了好几个小时在用户友好性上。加了几个progressBar,有显示总进度的、有显示分布进度的。。因为程序要跑好几个小时才能执行好,只加个总进度的话感觉不太明显。一个label,显示当先操作的具体内容,还有个richtextbox显示出所有操作哦。。

   直接扔代码咯,接好:

1 this.Invoke(new MethodInvoker(delegate() {  label1.Text = "正在处理... "; label1.Refresh(); }));

把它放在backgroundWorker1_DoWork()就能实现后台进程管理label内容。

在对richtextbox操作是,我起初使用的也是这个代码,但运行时才发现,richtextbox的光标不会自动下移。所以还要加点代码:

1 this.Invoke(new MethodInvoker(delegate() {
2 richTextBox1.AppendText(text); 
3 richTextBox1.SelectionStart = richTextBox1.Text.Length;
4 richTextBox1.ScrollToCaret();}));

那个.Refresh()被我省略掉了,但运行效果没有变化耶~~

附上个效果图吧:

 

 

转载于:https://www.cnblogs.com/WMTcore/p/3841055.html

展开阅读全文
博主设置当前文章不允许评论。

如何更改窗体LABEL内容

05-11

我这里有一个主窗体 和一个 DIALOG。 在DIALOG中设置好内容,然后关闭DIALOG,主窗体上LABEL的内容也更改为DIALOG中设置的内容,请问怎么实现?我这样写实现不了(这只是一个例子,真正的程序比这个复杂。如果要重新NEW主窗体的话 会很麻烦。因为窗体上还有很多其他的内容要更改。但不是同时更改的)。rn[code=Java]import java.awt.event.ActionEvent;rnimport java.awt.event.ActionListener;rnimport javax.swing.JButton;rnimport javax.swing.JFrame;rnimport javax.swing.JLabel;rnrnrnpublic class T_Main rnrn private JFrame frame;rn private JLabel label;rn private JLabel label_1;rnrn /**rn * Launch the applicationrn * @param argsrn */rn public static void main(String args[]) rn try rn T_Main window = new T_Main();rn window.frame.setVisible(true);rn catch (Exception e) rn e.printStackTrace();rn rn rnrn /**rn * Create the applicationrn */rn public T_Main() rn createContents();rn rnrn /**rn * Initialize the contents of the framern */rn private void createContents() rn frame = new JFrame();rn frame.getContentPane().setLayout(null);rn frame.setBounds(100, 100, 500, 375);rn frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);rnrn setLabel(new JLabel());rn getLabel().setText("New JLabel");rn getLabel().setBounds(93, 53, 66, 18);rn frame.getContentPane().add(getLabel());rnrn setLabel_1(new JLabel());rn getLabel_1().setText("New JLabel");rn getLabel_1().setBounds(93, 97, 66, 18);rn frame.getContentPane().add(getLabel_1());rnrn final JButton modifyButton = new JButton();rn modifyButton.addActionListener(new ActionListener() rn public void actionPerformed(final ActionEvent arg0) rn O o=new O();rn o.setVisible(true);rn rn );rn modifyButton.setText("Modify");rn modifyButton.setBounds(93, 185, 106, 28);rn frame.getContentPane().add(modifyButton);rn rnrn public void setLabel(JLabel label) rn this.label = label;rn rnrn public JLabel getLabel() rn return label;rn rnrn public void setLabel_1(JLabel label_1) rn this.label_1 = label_1;rn rnrn public JLabel getLabel_1() rn return label_1;rn rnrnrn[/code]rn[code=Java]import java.awt.event.ActionEvent;rnimport java.awt.event.ActionListener;rnimport javax.swing.JButton;rnrnimport javax.swing.JDialog;rnimport javax.swing.JTextField;rnrnrnpublic class O extends JDialog rnrn private JTextField textField_1;rn private JTextField textField;rnrn /**rn * Create the dialogrn */rn public O() rn super();rn setResizable(false);rn setModal(true);rn getContentPane().setLayout(null);rn setBounds(100, 100, 346, 306);rnrn textField = new JTextField();rn textField.setBounds(87, 87, 87, 22);rn getContentPane().add(textField);rnrn textField_1 = new JTextField();rn textField_1.setBounds(87, 142, 87, 22);rn getContentPane().add(textField_1);rnrn final JButton button = new JButton();rn button.addActionListener(new ActionListener() rn public void actionPerformed(final ActionEvent arg0) rn T_Main t=new T_Main();rn t.getLabel().setText(textField.getText());rn t.getLabel_1().setText(textField_1.getText());rn dispose();rn rn );rn button.setText("New JButton");rn button.setBounds(89, 203, 106, 28);rn getContentPane().add(button);rn //rn rnrnrn[/code] 论坛

backgroundWorker如何在后台启动监控进程

12-05

一个初始画面添加一个backgroundWorker后台,来处理初始化处理,然后想开始循环任务rn初始化进行的如何可以用ReportProgress来报告显示,没有问题后还想在后台开启循环任务,然后把处理结果rn报告给画面来显示,然而出错System.InvalidOperationException,始循环任务没有被正常启动。rn以下是部分例子代码rn[code=csharp]using System;rnusing System.Collections.Generic;rnusing System.ComponentModel;rnusing System.Data;rnusing System.Drawing;rnusing System.Linq;rnusing System.Text;rnusing System.Threading;rnusing System.Threading.Tasks;rnusing System.Windows.Forms;rn//主页面程序rnnamespace BGThreadrnrn public partial class Form1 : Formrn rn public Form1()rn rn InitializeComponent();rn backgroundWorker1.RunWorkerAsync();rn rnrn private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)rn rn backgroundWorker1.ReportProgress(0, "start Form init--"); rn Console.WriteLine("start Form init--");rn Thread.Sleep(5000);rnrn LoopWork lw = new LoopWork(backgroundWorker1);rnrnrn rnrn private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)rn rn string msg = (string)e.UserState;rn textBox1.Text = msg;rn rn rnrn[/code]rnrn[code=csharp]rnusing System;rnusing System.Collections.Generic;rnusing System.ComponentModel;rnusing System.Linq;rnusing System.Text;rnusing System.Threading;rnusing System.Threading.Tasks;rnrn//定期循环任务rnnamespace BGThreadrnrn public class LoopWorkrn rn private BackgroundWorker bgWorker;rn public LoopWork(BackgroundWorker bgWorker)rn rn this.bgWorker = bgWorker;rn init();rn rnrn private void init()rn rn task1();rn task2();rnrn rnrn private void task1()rn rn Task.Factory.StartNew(() =>rn rn while (true)rn rn bgWorker.ReportProgress(0, "taks1--" + DateTime.Now.ToString("hh:mm:ss"));rn Console.WriteLine("taks1--" + DateTime.Now.ToString("hh:mm:ss"));rn Thread.Sleep(new TimeSpan(0, 0, 10));rn rn );rn rn private void task2()rn rn Task.Factory.StartNew(() =>rn rn while (true)rn rn bgWorker.ReportProgress(0, "taks2--" + DateTime.Now.ToString("hh:mm:ss"));rn Console.WriteLine("taks2--" + DateTime.Now.ToString("hh:mm:ss"));rn Thread.Sleep(new TimeSpan(0, 0, 15));rn rn );rn rnrn rnrnrn[/code]rn 论坛

没有更多推荐了,返回首页