Home Assistant | Unifi | LG 51MR.U44 | Volvo EX30 SMER+ Vapour Grey, trekhaak | SmartEVSE V3 | Cronos Crypto.com
Help me eens op weg... Ik heb alleen een C# voorbeeld gevonden, maar het lukte me niet om dat om te zetten naar VB...PhoneTech schreef op 26 december 2003 @ 14:17:
dat kan alleen met delegates
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
| // this variable will hold some text set by the worker thread
public string Message = "";
// Create a worker thread and then add items to the ListBox from the
// UI thread
public void DoThreading()
{
// Create the worker thread and start it
ThreadStart starter = new ThreadStart(this.UpdateListBox);
Thread t = new Thread(starter);
t.Start();
// Loop 4 times, adding a message to the ListBox each time
for(int i = 0; i < 4; i++);
{
this.listBox1.Items.Add("Message from UI thread");
this.listBox1.Update();
// Process any queued events on the UI thread
Application.DoEvents();
// Suspend processing for 1 second
Thread.Sleep(1000);
}
this.listBox1.Items.Add("Last message from UI thread");
this.listBox1.Update();
}
public void UpdateListBox()
{
for(int j = 0; j < 5; j++)
{
// Set the message to be added to the ListBox from the worker
// thread
this.Message = "Worker thread loop count = " + j.ToString();
// Invoke the WorkerUpdate method in the ListBox's thread
// context
this.listBox1.Invoke(new EventHandler(WorkerUpdate));
Thread.Sleep(700);
}
}
// The delegate that's called from the worker thread
// to update the ListBox
public void WorkerUpdate(object sender, EventArgs e)
{
this.listBox1.Items.Add(this.Message);
this.listBox1.Update();
} |
[ Voor 75% gewijzigd door Swerfer op 26-12-2003 14:54 ]
Home Assistant | Unifi | LG 51MR.U44 | Volvo EX30 SMER+ Vapour Grey, trekhaak | SmartEVSE V3 | Cronos Crypto.com
Als ik de C# code bekijk, en dan vooral dit stukje;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
| public void UpdateListBox()
{
for(int j = 0; j < 5; j++)
{
// Set the message to be added to the ListBox from the worker
// thread
this.Message = "Worker thread loop count = " + j.ToString();
// Invoke the WorkerUpdate method in the ListBox's thread
// context
this.listBox1.Invoke(new EventHandler(WorkerUpdate));
Thread.Sleep(700);
}
}
// The delegate that's called from the worker thread
// to update the ListBox
public void WorkerUpdate(object sender, EventArgs e)
{
this.listBox1.Items.Add(this.Message);
this.listBox1.Update();
} |
dan denk ik dat
1
| this.listBox1.Invoke(new EventHandler(WorkerUpdate)); |
Dit is wat ik dus wil bereiken in VB.
Nu heb ik het stuk over delegates gelezen, maar het lukt me niet om de C# code om te zetten naar VB met behulp van dat stuk over delegates...
Mijn code zou er in C# denk ik zo uit moeten zien:
1
2
3
4
5
6
7
8
9
10
| public void myThread()
{
.....
this.button1.Invoke(new EventHandler(ShowButton));
}
public void ShowButton(object sender, EventArgs e)
{
this.button1.Visible = true;
} |
Maar hoe moet nou die code omgezet worden naar VB?
[ Voor 4% gewijzigd door Swerfer op 26-12-2003 15:55 ]
Home Assistant | Unifi | LG 51MR.U44 | Volvo EX30 SMER+ Vapour Grey, trekhaak | SmartEVSE V3 | Cronos Crypto.com
Je code zal er, in C#, ongeveer zo moeten uitzien:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
| public class MyThreadedObject
{
private Thread TheThread;
public event DoSomethingDelegate;
public StartThread()
{
TheThread = new Thread(new ThreadStart(TheMethodThatRunsInAThread));
TheThread.Start();
}
private void TheMethodThatRunsInAThread()
{
// do some stuff here
// ......
// call the delegate (if there's a method assigned)
if( DoSomethingDelegate != null )
{
DoSomethingDelegate(this, null);
}
}
} |
Deze class bevat dus gewoon een Thread object, en een 'event'. Als je de StartThread() method aanroept, dan wordt er de private method van die class in een andere thread uitgevoerd, en als er een handler aan die event hangt, dan wordt die ook uitgevoerd.
Met deze code ga je die thread bv. starten:
1
2
3
4
5
| MyThreadedObject t; t = new MyThreadedObject(); t.DoSomethingDelegate += new EventHandler(EnableTheButton); t.StartThread(); |
1
2
3
4
| public void EnableTheButton(object sender, EventArgs e )
{
this.Button1.Visible = true;
} |
Nouja, bij nader inzien is het eigenlijk niet nodig om een aparte class te maken.
[ Voor 3% gewijzigd door whoami op 26-12-2003 16:10 ]
https://fgheysels.github.io/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
| Public Class Form1 Dim EndOfThread As EventHandler Sub blabla() EndOfThread = New EventHandler(AddressOf ShowButton) Dim Thread1 As New Thread(AddressOf Thread) Thread1.IsBackground = True Thread1.Start() End Sub Sub Thread() .... .... Invoke(EndOfThread, New Object() {Me, EventArgs.Empty}) End Sub Sub ShowButton(ByVal sender As Object, ByVal e As EventArgs) Button1.Visible = True End Sub End Class |
Kan nog eenvoudiger:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
| Public Class Form1 Sub blabla() Dim Thread1 As New Thread(AddressOf Thread) Thread1.IsBackground = True Thread1.Start() End Sub Sub Thread() .... .... Invoke(New EventHandler(AddressOf ShowButton), _ New Object() {Me, EventArgs.Empty}) End Sub Sub ShowButton(ByVal sender As Object, ByVal e As EventArgs) Button1.Visible = True End Sub End Class |
[ Voor 32% gewijzigd door Swerfer op 28-12-2003 16:34 ]
Home Assistant | Unifi | LG 51MR.U44 | Volvo EX30 SMER+ Vapour Grey, trekhaak | SmartEVSE V3 | Cronos Crypto.com