473,385 Members | 1,712 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

New to threading. Need suggestions

MS
Hi. I'm new to developing and c#.

I'm developing a tool that allows a user to select a directory with
specially formatted files. The tool then iterates through the list of files
in the directory and sends their content via msmq. The part that iterates
and sends the files is in a separate thread. I have a button on the form
that allows the user to pause or resume the sending of these files. I'm
currently using thread.suspend and thread.resume for this. In the .NET
Framework 2.0 documentation it states that these methods are now deprecated.

How else can I achieve this functionality without using these methods?

Thanks!
Nov 23 '05 #1
4 1442
MS,

I would use a ManualResetEvent for this. Basically, you would create
the ManualResetEvent initially setting the state to signaled (there is a
constructor that you can pass true to in order to achieve this).

Then, in your worker thread, on each iteration of the loop, you call
WaitOne on the event (the event is shared between the threads, obviously).
Since it is a ManualResetEvent, the call to WaitOne will return immediately.

When you want to pause, you just call Reset on the ManualResetEvent, and
it will set the event to non-signaled. Then, on the next iteration of the
loop, your thread will pause. When you want to resume, just call Set on the
event.

This is actually cleaner than using Suspend and Resume, since it will
allow you to go through one iteration of your loop, and not leave file
handles open and the like.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"MS" <ma*****@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
Hi. I'm new to developing and c#.

I'm developing a tool that allows a user to select a directory with
specially formatted files. The tool then iterates through the list of
files in the directory and sends their content via msmq. The part that
iterates and sends the files is in a separate thread. I have a button on
the form that allows the user to pause or resume the sending of these
files. I'm currently using thread.suspend and thread.resume for this. In
the .NET Framework 2.0 documentation it states that these methods are now
deprecated.

How else can I achieve this functionality without using these methods?

Thanks!

Nov 23 '05 #2
MS
Thanks Nicholas. I got it working using ManualResetEvent even though I don't
quite understand what it does. I'll read up on it. This represents what I
have on my form. Am I doing it right?

private class form....
ManualResetEvent manualEvent;.
..
..
private void InitializeComponent()
this.manualEvent = new ManualResetEvent(true);
..
..
private void ProcessFiles...
create new thread and start it (runs method called Send)
..
..
private void send....
{
manualEvent.Set()
foreach(file in collection)
{
send the file;
manualEvent.WaitOne();
}
}

private void btnPause()
{
if(not paused)
manualEvent.Reset();
else
manualEvent.Set();
}
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:ue**************@TK2MSFTNGP09.phx.gbl...
MS,

I would use a ManualResetEvent for this. Basically, you would create
the ManualResetEvent initially setting the state to signaled (there is a
constructor that you can pass true to in order to achieve this).

Then, in your worker thread, on each iteration of the loop, you call
WaitOne on the event (the event is shared between the threads, obviously).
Since it is a ManualResetEvent, the call to WaitOne will return
immediately.

When you want to pause, you just call Reset on the ManualResetEvent,
and it will set the event to non-signaled. Then, on the next iteration of
the loop, your thread will pause. When you want to resume, just call Set
on the event.

This is actually cleaner than using Suspend and Resume, since it will
allow you to go through one iteration of your loop, and not leave file
handles open and the like.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"MS" <ma*****@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
Hi. I'm new to developing and c#.

I'm developing a tool that allows a user to select a directory with
specially formatted files. The tool then iterates through the list of
files in the directory and sends their content via msmq. The part that
iterates and sends the files is in a separate thread. I have a button on
the form that allows the user to pause or resume the sending of these
files. I'm currently using thread.suspend and thread.resume for this. In
the .NET Framework 2.0 documentation it states that these methods are now
deprecated.

How else can I achieve this functionality without using these methods?

Thanks!


Nov 23 '05 #3
MS,

You are almost there. In the send method (which is called on the non-UI
thread, I assume), you don't want to call Set before you start looping.

Also, you don't want to call WaitOne after you send the file, but
before. This way, if you end, and hit pause before you finish, you dont
hold up the thread when there is nothing left to do.

Finally, call Set before you start the thread, and after that, you
should be fine.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"MS" <ma*****@hotmail.com> wrote in message
news:Oi**************@TK2MSFTNGP11.phx.gbl...
Thanks Nicholas. I got it working using ManualResetEvent even though I
don't quite understand what it does. I'll read up on it. This represents
what I have on my form. Am I doing it right?

private class form....
ManualResetEvent manualEvent;.
.
.
private void InitializeComponent()
this.manualEvent = new ManualResetEvent(true);
.
.
private void ProcessFiles...
create new thread and start it (runs method called Send)
.
.
private void send....
{
manualEvent.Set()
foreach(file in collection)
{
send the file;
manualEvent.WaitOne();
}
}

private void btnPause()
{
if(not paused)
manualEvent.Reset();
else
manualEvent.Set();
}
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote
in message news:ue**************@TK2MSFTNGP09.phx.gbl...
MS,

I would use a ManualResetEvent for this. Basically, you would create
the ManualResetEvent initially setting the state to signaled (there is a
constructor that you can pass true to in order to achieve this).

Then, in your worker thread, on each iteration of the loop, you call
WaitOne on the event (the event is shared between the threads,
obviously). Since it is a ManualResetEvent, the call to WaitOne will
return immediately.

When you want to pause, you just call Reset on the ManualResetEvent,
and it will set the event to non-signaled. Then, on the next iteration
of the loop, your thread will pause. When you want to resume, just call
Set on the event.

This is actually cleaner than using Suspend and Resume, since it will
allow you to go through one iteration of your loop, and not leave file
handles open and the like.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"MS" <ma*****@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
Hi. I'm new to developing and c#.

I'm developing a tool that allows a user to select a directory with
specially formatted files. The tool then iterates through the list of
files in the directory and sends their content via msmq. The part that
iterates and sends the files is in a separate thread. I have a button on
the form that allows the user to pause or resume the sending of these
files. I'm currently using thread.suspend and thread.resume for this. In
the .NET Framework 2.0 documentation it states that these methods are
now deprecated.

How else can I achieve this functionality without using these methods?

Thanks!



Nov 23 '05 #4
MS wrote:
Hi. I'm new to developing and c#.

I'm developing a tool that allows a user to select a directory with
specially formatted files. The tool then iterates through the list of files
in the directory and sends their content via msmq. The part that iterates
and sends the files is in a separate thread. I have a button on the form
that allows the user to pause or resume the sending of these files. I'm
currently using thread.suspend and thread.resume for this. In the .NET
Framework 2.0 documentation it states that these methods are now deprecated.

How else can I achieve this functionality without using these methods?

Thanks!


The reason Suspend and Resume is deprecated is that you're suspending
the hardware thread, at whatever position it is currently being
executed. If you suspend it inside a lock(){...} block, that lock will
not be released until you resume the thread.

In other words, outside code is able to destabilize your system by
making locks stay longer than necessary.

By using events like the others suggested you yourself adds the points
where your thread is safe to be "suspended". For instance, what if the
code that talks to MSMQ is suspended in the middle? Will MSMQ time out
after a while and when you resume the thread it might not be able to
complete the current transaction? Such things is why they have become
deprecated.

--
Lasse Vågsæther Karlsen
http://usinglvkblog.blogspot.com/
mailto:la***@vkarlsen.no
PGP KeyID: 0x2A42A1C2
Nov 24 '05 #5

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

Similar topics

77
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...
1
by: Carl Waldbieser | last post by:
I have been considering using Python and the Reportlab library for generating PDF reports for the back-end of a web based application. The application runs most of its background tasks on a...
2
by: Montaque | last post by:
//Refersh the datagrid automatically.however,the callback function test() is not been invoked. any suggestions? the following lists the code private void AutoRefreshData() {...
8
by: Z D | last post by:
Hello, I'm having a strange problem that is probably due to my lack of understanding of how threading & COM Interop works in a WinForms.NET application. Here's the situation: I have a 3rd...
4
by: Chris Johnson | last post by:
Ill be the first to admit this is way beyond my current scope as a VB programmer but Im learning as I go. I playing with the new Threading features exposed by the 2.0 framework and have writting...
8
by: WhiteWizard | last post by:
I guess it's my turn to ASK a question ;) Briefly my problem: I am developing a Windows app that has several User Controls. On one of these controls, I am copying/processing some rather large...
5
by: bean330 | last post by:
Hey, I'm somewhat new to C# and I need a little help, please! I'm selecting a bunch of records, setting properties on a COM executable and then calling a method on that executable to run. I...
2
by: PimpDaddy | last post by:
Here is the source code of my C++/ME app. When I run it I can use "Windows Task Manager" to see that thread HANDLEs are leaking. #include "stdafx.h" #include <omp.h> public __gc class...
5
by: George Maicovschi | last post by:
As multi-threading is not built in PHP I've been using a hack letting the Apache server handle the multi-threading issues, but I'm really curious of other approaches to this issue. If anyone has...
7
by: =?Utf-8?B?QW5keSBXaWxsaWFtcw==?= | last post by:
Apologies in advance for the long post. I have an application which has 2 CheckedListBox controls, CLB 1 is a list of Directories and CLB 2 is a list of Files. Directories can be added to CLB...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.