Sinisterly
I have a problem with Background worker in C# - Printable Version

+- Sinisterly (https://sinister.ly)
+-- Forum: Coding (https://sinister.ly/Forum-Coding)
+--- Forum: Visual Basic & .NET Framework (https://sinister.ly/Forum-Visual-Basic-NET-Framework)
+--- Thread: I have a problem with Background worker in C# (/Thread-I-have-a-problem-with-Background-worker-in-C)



I have a problem with Background worker in C# - leam_lidara - 05-24-2012

Any problem with that code below? I have a problem while I try to show form or any thing related to form such as textbox, listbox, button.... Any command? Thank before hands.
Code:
private void Timer_Tick(object sender, EventArgs e) { if (backgroundWorker1.IsBusy == false) { backgroundWorker1.RunWorkerAsync(this); } } private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) { try{ this.Show(); this.notifyIcon1.Visible = false; this.ShowInTaskbar = true; this.WindowState = FormWindowState.Normal; }catch(Exception ex) { MessageBox.Show(ex.Message); } }



RE: I have a problem with Background worker in C# - ArkPhaze - 05-25-2012

What exactly are you trying to do here with the background worker?


RE: I have a problem with Background worker in C# - leam_lidara - 05-25-2012

I want to show form with it and use other tools such as textbox but I have a problem and cannot access to show form or cannot access to use other method of textbox.[/align]


RE: I have a problem with Background worker in C# - ArkPhaze - 05-26-2012

Why are you doing this in a background worker just to show a form? If you want to show the form and do it in a multi-threaded method:

Code:
new Thread(x => new Form1().Show());



RE: I have a problem with Background worker in C# - leam_lidara - 05-26-2012

It's not work..... any thing else?


RE: I have a problem with Background worker in C# - ArkPhaze - 05-26-2012

What do you mean not working? Post the code you have.


RE: I have a problem with Background worker in C# - leam_lidara - 05-27-2012

I think that Background Worker doesn't work with Windows Form component! So I need your suggestion to use Background worker with Windows Form component.


RE: I have a problem with Background worker in C# - ArkPhaze - 05-28-2012

Sorry late night yesterday for me, I didn't even test the code I gave before because I was just about to head to sleep (start work again today). Invoke it to create a new instance of the form.

Code:
public Form1() { InitializeComponent(); bg_worker.DoWork += new DoWorkEventHandler(bg_work); bg_worker.ProgressChanged += new ProgressChangedEventHandler(bg_progress); bg_worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bg_completed); } public BackgroundWorker bg_worker = new BackgroundWorker(); private unsafe void button1_Click(object sender, EventArgs e) { bg_worker.RunWorkerAsync(); } private void bg_work(object sender, EventArgs e) { this.Invoke((MethodInvoker)delegate { new Form1().Show(); }); } private void bg_progress(object sender, EventArgs e) { //Progress for other things here } private void bg_completed(object sender, EventArgs e) { //Completed code here bg_worker.Dispose(); }



RE: I have a problem with Background worker in C# - leam_lidara - 05-29-2012

Ohhh!!! Ok!! Thank you in advance!!! It's work for me!