473,503 Members | 1,731 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C# Threading

Under my button click event, I have code that will call another method that takes 3-4 seconds. I want to be call that method and be free to do something else. I know this can be done with threading, but I just can't seem to get it right. Below is what I have so far. The Join statement is what is holding up the code.

Any suggesstions

Thanks

private void btnSend_Click(object sender, System.EventArgs e

this.Cursor = System.Windows.Forms.Cursors.WaitCursor;

Thread thread1 = new Thread(new ThreadStart(sendMsg))
thread1.Name= "JMS"
thread1.Start();

int count=0
thread1.Join();

while (this.msgReceived == false

count++
Console.WriteLine(thread1.ThreadState.ToString())
Console.WriteLine("sendMsg: " + count);

Jul 21 '05 #1
8 2123
Jason <an*******@discussions.microsoft.com> wrote:
Under my button click event, I have code that will call another
method that takes 3-4 seconds. I want to be call that method and be
free to do something else. I know this can be done with threading,
but I just can't seem to get it right. Below is what I have so far.
The Join statement is what is holding up the code.


Yes - the Join statement waits for the other thread to die. Without the
call to Join, your thread is free to do other stuff. Why did you have
the call to Join in there?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #2
Because that was the only way that the method that I was calling would work.
Jul 21 '05 #3
Jason <an*******@discussions.microsoft.com> wrote:

[It helps if you quote some of what you're replying to]
Because that was the only way that the method that I was calling
would work.


Well, the method you were calling would execute either way. Thread.Join
is just going to make the thread wait until that other thread has
finished. What were you actually trying to do? In what way didn't it
work without the call to Thread.Join?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #4
Jason <an*******@discussions.microsoft.com> wrote:
OK, here is what I want to do. It is real simple. I want my button
click event to call the sendMsg method, which takes about 2-3 seconds
to execute and then still be able to execute other code in the button
click event such as the looping code listed below. Is this possible
to do?


The problem is with the "execute other code in the button click event".
While your button click event is executing, no other UI events can
occur. The correct way round this is to make the button click event
just start the other thread, and make the other thread itself tell the
UI that it's finished by invoking a delegate (using Control.Invoke or
Control.BeginInvoke).

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #5
Sounds good. Can you point me into the right direction in order to setup the delegates for the other thread to use?
Jul 21 '05 #6
Jason <an*******@discussions.microsoft.com> wrote:
Sounds good. Can you point me into the right direction in order to
setup the delegates for the other thread to use?


You can create any delegate you like, although you may want to make it
follow the EventHandler pattern (object sender, EventArgs e). (I
believe EventHandlers are more efficient for Control.Invoke, although I
wouldn't put too much stock in that.)

I'm writing an article about multithreading which will soon cover
Windows Forms and give an example of this kind of thing - hopefully
over the weekend it'll be finished. It's at
http://www.pobox.com/~skeet/csharp/multithreading.html and is gradually
getting there...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #7
Cool, about when do you think the information will be available?
Jul 21 '05 #8
Jason <an*******@discussions.microsoft.com> wrote:
Cool, about when do you think the information will be available?


As I said, some time over the weekend.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

65
6663
by: Anthony_Barker | last post by:
I have been reading a book about the evolution of the Basic programming language. The author states that Basic - particularly Microsoft's version is full of compromises which crept in along the...
2
2953
by: Egor Bolonev | last post by:
hi all my program terminates with error i dont know why it tells 'TypeError: run() takes exactly 1 argument (10 given)' =program==================== import os, os.path, threading, sys def...
77
5208
by: Jon Skeet [C# MVP] | last post by:
Please excuse the cross-post - I'm pretty sure I've had interest in the article on all the groups this is posted to. I've finally managed to finish my article on multi-threading - at least for...
6
555
by: CK | last post by:
I have the following code in a windows service, when I start the windows service process1 and process2 work fine , but final process (3) doesnt get called. i stop and restart the windows service...
2
2231
by: Vjay77 | last post by:
In this code: Private Sub downloadBtn_Click(ByVal sender As Object, ByVal e As System.EventArgs) If Not (Me.downloadUrlTextBox.Text = "") Then Me.outputGroupBox.Enabled = True...
11
4988
by: Paul Sijben | last post by:
I am stumped by the following problem. I have a large multi-threaded server accepting communications on one UDP port (chosen for its supposed speed). I have been profiling the code and found...
17
6389
by: OlafMeding | last post by:
Below are 2 files that isolate the problem. Note, both programs hang (stop responding) with hyper-threading turned on (a BIOS setting), but work as expected with hyper-threading turned off. ...
0
1569
by: kingcrowbar.list | last post by:
Hello Everyone I have been playing a little with pyGTK and threading to come up with simple alert dialog which plays a sound in the background. The need for threading came when in the first...
7
2348
by: Mike P | last post by:
I am trying to write my first program using threading..basically I am moving messages from an Outlook inbox and want to show the user where the process is up to without having to wait until it has...
126
6607
by: Dann Corbit | last post by:
Rather than create a new way of doing things: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2497.html why not just pick up ACE into the existing standard:...
0
7334
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6993
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5579
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5014
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4675
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3168
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3156
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1514
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
383
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.