0

I have to run a thread create in the code. In the form1 i have a button that run the new separate thread for elaborate some data, so i need it for not freeze the form.

I have inizialized thread:

dim th as thread = new thread (addressof elaborate)

And at the button.click event:

th.isbackground= true
th.start()

Now, at the form load i have iconized my program, but when i start new thread the tray icon is duplicated from it. I want to resolve that when start new thread it's not show new notifyicon.

Any ideas?

(i don't have found anything online, only Multiple notification icons appear when using multithreading)

Community
  • 1
  • 1
  • possible duplicate of [Multiple notification icons appear when using multithreading](http://stackoverflow.com/questions/5989648/multiple-notification-icons-appear-when-using-multithreading) – cHao Sep 24 '11 at 12:24
  • Do not display forms on worker threads. Use BackgroundWorker to execute code that takes time and causes the UI to freeze. – Hans Passant Sep 24 '11 at 14:23
  • @cHao I have already read the answer, but i don't understood how fix the problem. I have notice that the problem are fix if i delete any call of form. But in this case, how i can call a sub in form? – Inconsapevole Sep 25 '11 at 15:46

1 Answers1

0

Create a class called Elab inside that class, put a sub called work Add a timer to your form that is disabled

with a tickcount of say 1000

Declare this in your form class:

Dim El as Elab

inside Form_Load() put:

El = New Elab()

Under your button, put this:

Dim gThread as new System.Threading.Thread(Address of El.Work)
Timer1.Enabled = True

Inside Elab declare a variable called Result:

Public Result as boolean

When elab has finished whatever it is doing, set result as true, and store the results in public variables you can access later.

Inside the timer:

If El.Result = True then
   'Get results, deal with data
end if

This isn't written particularly well, and isn't a working example, but is to mearly point you in the right direction, by giving a thread an address of a sub inside a class, you can then access the same class from other threads, which means your form doesn't freeze, and you're not creating a new instance of your form, you are just accessing an existing classes sub routine; just make sure to give yourself a way to get the results (in this example i suggested a timer, but a "get result" button would do the same job) once the thread has completed.

Remeber: If you need Elab to finish before a particular part of code can continue (for example, elab might add two numbers, and you need the result to continue) you can start the thread and do this:

Do until El.Result = True
    Application.DoEvents()
    System.Threading.Thread.Sleep(1)
Loop

gThread.Join()

Hope this helps a little.

So the answer to your question is; don't put your sub inside a form, instead put it in a class that you can create an instance of.

Danny
  • 53
  • 1
  • 7