开发者

i written a winforms application in VB .NET in visual studio 2010

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 th开发者_JAVA百科read 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)


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜